美文网首页
SQL练习题

SQL练习题

作者: luckee | 来源:发表于2019-01-19 19:50 被阅读0次
    • sql exercise-1
    • like/not like
    • in/not in
    • exists/not exists
    • any/all
      举例:
    t_book
    t_booktype
    t_price
    SELECT * FROM t_book WHERE bookTypeId IN (SELECT id FROM t_booktype);
    
    SELECT * FROM t_book WHERE price >=(SELECT price FROM t_price WHERE priceLevel=1);
    
    #只有当后面有查询结果,也就是返回true的时候才会执行前面的查询
    SELECT * FROM t_book WHERE EXISTS (SELECT * FROM t_booktype);
    
    #大于等于任意一个即可,即大于等于最小值就可以
    SELECT * FROM t_book WHERE price >= ANY(SELECT price FROM t_price);
    
    #大于等于所有的,即大于等于最大值才可以
    SELECT * FROM t_book WHERE price >= ALL(SELECT price FROM t_price);
    

    别名

    • 字段别名
      select id [as] '编号' from t_item(as 可省略)
    • 表别名
      select it.id, it.name from t_item [as] it(as 可省略)
    • 结果集别名
      select it.id, it.name from (select * from t_item where price>100) it
      面试的时候做的一道笔试题,有一张项目进度表project_progress,内容如下:
    project_name(项目名称) progress_name(进度) date(日期)
    项目1 申请 2019-1-1
    项目2 申请 2019-1-5
    项目1 预审 2019-1-10

    使用SQL语句将其变成:

    项目名称 申请 预审
    项目1 2019-1-1 2019-1-10
    项目2 2019-1-5
    select s1.project_name as 项目名称, s1.date as 申请, s2.date as 预审 from
    (
    (select project_name, date from project_progress where progress_name = '申请') s1 
    left join 
    (select project_name, date from project_progress where progress_name = '预审') s2
    on s1.project_name = s2.project_name
    );
    

    or

    select s1.项目名称, 申请,  预审 from
    (
    (select project_name as 项目名称, date as 申请 from project_progress where progress_name = '申请') s1 
    left join 
    (select project_name as 项目名称, date as 预审 from project_progress where progress_name = '预审') s2
    on s1.项目名称 = s2.项目名称
    );
    

    相关文章

      网友评论

          本文标题:SQL练习题

          本文链接:https://www.haomeiwen.com/subject/bvwzdqtx.html