SQL实例学习(一)

检索记录

检索所有行和列

  1. 代码

    # 查询表中所有数据
    select * from EMP;
    
    # 查询具体制定的列
    select EMPNO, ENAME, JOB, SAL, MGR, HIREDATE, COMM, DEPTNO from EMP;
    

筛选行

  1. 代码

    # 筛选行, 查看满足条件的行
    select * from EMP where DEPTNO = 10;
    

查找满足多个查询条件的行

  1. 代码

    # 查找满足多个条件的行
    select *
    from EMP
    where DEPTNO = 10
       or COMM is not null
       or SAL <= 2000 and DEPTNO = 20;
    

筛选列

  1. 代码

    # 筛选列
    select ENAME, DEPTNO, SAL
    from EMP;
    

创建有意义的列名

  1. 代码

    # 创建有意义的列名,使用AS创建别名
    select SAL as salary, COMM as commission
    from EMP;
    

在where子句中引用别名列

  1. 错误代码

    # 执行会报错
    select SAL as salary, COMM as commission 
    from EMP
    where salary < 5000;
    
  2. 正确代码,使用内嵌视图

    # 使用别名
    select *
    from (select SAL as salary, COMM as commission from EMP) x
    where salary < 5000;
    
  3. 说明, where子句会比select子句先执行,就失败的例子而言,当where子句被执行时,salary和commission尚不存在。直到where子句执行完毕,别名才会生效。from子句会先于where子句查询。

串联多列的值

  1. 查找结果的值

    ENAME JOB
    CLARK MANAGER
    KING PRESIDENT
    MILLER CLERK
  2. 想要的结果

    CLARK WORKS AS AMANAGER
    KING WORKS AS APRESIDENT
    MILLER WORKS AS ACLERK
    
  3. 代码

    # 串联多列的值
    # CONCAT函数可以串联多列的值
    select concat(ENAME, ' WORKS AS A', JOB) as msg
    from EMP
    where DEPTNO = 10;
    

在select语句里使用条件逻辑

  1. 代码

    # select语句里使用条件逻辑
    select ENAME,
           SAL,
           case
             when SAL <= 2000 then 'UNDERPAID'
             when SAL >= 4000 then 'OVERPAID'
             else 'OK'
               end as status
    from EMP;
    
  2. 结果

    ENAME SAL STATUS
    SMITH 800 UNDERPAID
    ALLEN 1600 UNDERPAID
    WARD 1250 UNDERPAID
    JONES 2975 OK
    MARTIN 1250 UNDERPAID
    BLAKE 2850 OK
    CLARK 2450 OK
    SCOTT 3000 OK
    KING 5000 OVERPAID
    TURNER 1500 UNDERPAID
    ADAMS 1100 UNDERPAID
    JAMES 950 UNDERPAID
    FORD 3000 OK
    MILLER 1300 UNDERPAID

限定返回行数

  1. 代码

    # 限定返回的行数
    select *
    from EMP
    limit 5;
    

随机返回若干行记录

  1. 代码

    # 随机返回若干行记录
    select ENAME, JOB
    from EMP
    order by rand()
    limit 5;
    

查找null值

  1. 代码

    # 查找null值
    select *
    from EMP
    where COMM is null;
    

把null值转换为实际值

  1. 代码

    # 把null值转换为实际值
    # coalesce函数会返回参数列表里的第一个非null值。
    select coalesce(COMM, 0), ENAME
    from EMP;
    
    # 使用case
    select case
             when COMM is not null then COMM
             else 0 end
    from EMP;
    

查找匹配项

  1. 代码

    # 查询匹配项
    # '%I%'表示任意位置出现I的字符串都会检索出来,'%ER'表示检索以ER结尾的字符串
    select ENAME, JOB
    from EMP
    where DEPTNO in (10, 20)
      and (ENAME like '%I%' or JOB like '%ER');
    

查询结果排序

指定顺序返回查询结果

  1. 代码

    # sal从小到大
    select ENAME, JOB, SAL
    from EMP
    where DEPTNO = 10
    order by SAL asc;
    

多字段排序

  1. 代码

    # 先按照DEPTNO升序,然后再按照SAL降序排列
    select EMPNO, DEPTNO, SAL, ENAME, JOB
    from EMP
    order by DEPTNO, SAL desc;
    

依据子句排序

  1. 代码

    # 按照职位字段的最后两个字符对检索结果进行结果
    select ENAME, JOB
    from EMP
    order by substr(JOB, length(JOB) - 2);
    

排序时对null值的处理

  1. 代码

    # 排序时对null值的处理
    # 添加辅助列进行排序
    select ENAME, SAL, COMM
    from (select ENAME, SAL, COMM, case when COMM is null then 0 else 1 end as is_null from EMP) x
    order by is_null desc, COMM;
    

根据条件逻辑动态调整排序项

  1. 代码

    # 根据条件逻辑动态调整排序项
    select ENAME, SAL, JOB, COMM
    from EMP
    order by case when JOB = 'SALESMAN' then COMM else SAL end;
    

你可能感兴趣的:(SQL实例学习(一))