• select语句


    1.  查询语句

    1) select

    select用于从数据看查询数据。语法

    1 select field1,filed2,.. .
    2 
    3 from tablename
    4 
    5 [where condition]
     1 -- 查询所有员工的名字和雇员号
     2 
     3 select empno,ename from emp;
     4 
     5  
     6 
     7 -- 查询所有员工的雇员号、姓名、岗位
     8 
     9 select empno,ename,job from emp;
    10 
    11  
    12 
    13 -- 字段的别名 as
    14 
    15 select ename as "姓名" from emp;
    16 
    17 select ename as "姓名",job as "岗位" from emp;
    18 
    19  
    20 
    21 -- 别名一定要用双引号,不能用单引号
    22 
    23 select ename "姓名",job "岗位" from emp;
    24 
    25 -- 双引号可以省略
    26 
    27 select ename 姓名from emp;
    28 
    29  
    30 
    31 -- 表的别名
    32 
    33 select emp.ename,emp.job from emp;
    34 
    35 select e.ename,e.job from emp e; 

    *通配符表示查询所有字段。如果要查特定字段时,不要使用*,影响查询效率。

    select empno,ename,job,mgr,hiredate,sal,comm,deptno
    
    from emp;
    
    -- * 通配符表示所有字段
    
    select * from emp; 

    2) distinct去重

    把重复性的记录去掉,只保留一条。

    -- 查询公司的工种
    
    select distinct e.job
    
    from emp e;

    修饰多字段时,多个字段的值都不一样才保留。

    3)  where子句

    where表示查询的条件。

    [1] =,!= ,<>,<,>,<=,>= 关系运算符

    <>表示不等于

     1 -- where 子句
     2 
     3  
     4 
     5 -- 把部分10的雇员查询出来
     6 
     7 select *
     8 
     9 from emp
    10 
    11 where deptno = 10;
    12 
    13  
    14 
    15 -- 把名称为smith的雇员
    16 
    17 select e.*
    18 
    19 from emp e
    20 
    21 where e.ename = 'SMITH';
    22 
    23  
    24 
    25 -- 查询底薪大于等于1000的员工
    26 
    27 select e.*
    28 
    29 from emp e
    30 
    31 where e.sal >= 1000;
    32 
    33  
    34 
    35 select e.*
    36 
    37 from emp e
    38 
    39 where e.sal <>800

    any/some/all (list)

    any/some(list) 满足list列表中的任意一个条件

    all(list) 满足list列表的中所有条件

     1 -- any some all
     2 
     3  
     4 
     5 -- 查询薪资大于1000或者薪资大于800的雇员
     6 
     7 select e.*
     8 
     9 from emp e
    10 
    11 where e.sal >some(1000,800);
    12 
    13  
    14 
    15 -- 查询薪资大于1000
    16 
    17 select e.*
    18 
    19 from emp e
    20 
    21 where e.sal >all(1000,800);

    [2]null

    null 在sql中表示的是不确定 =>可以认为没有值

    -- null/not null
    
    -- 查询没有津贴的雇员
    
    select e.*
    
    from emp e
    
    where e.comm is null
    
     
    
    select e.*
    
    from emp e
    
    where e.comm is not null

    [3]between x and y

    表示一个值位于[x,y]区间,x/y 一般都是数字。头尾都包含

     1 -- between x and y
     2 
     3 -- 查询薪资在1000-5000之间的雇员
     4 
     5 select e.*
     6 
     7 from emp e
     8 
     9 where e.sal between1000and5000
    10 
    11  
    12 
    13 -- 查询薪资在(3000,5000]之间的雇员
    14 
    15 select e.*
    16 
    17 from emp e
    18 
    19 where e.sal between3000.01and5000

    [4]in/not in list

    表示字段值是否在list列表中

     1 -- in/not in(list)
     2 
     3 -- 查询部分号是10和20的员工
     4 
     5 select e.*
     6 
     7 from emp e
     8 
     9 where e.deptno in(10,20);
    10 
    11  
    12 
    13 select e.*
    14 
    15 from emp e
    16 
    17 where e.deptno notin(10,20);
    18 
    19  
    20 
    21 -- 查询薪资是1000,2000,5000的员工
    22 
    23 select e.*
    24 
    25 from emp e
    26 
    27 where e.sal in (1000,2000,5000);

    [5]模糊查询

    like关键字用于模糊查询,其中

    %:表示任意字符出现多次(含0次),

    _:表示任意字符出现1次。

    escape(‘x’)表示指定转义字符为x,一般指定为

     1 -- 查询名字是c开头的雇员
     2 
     3  
     4 
     5 select e.*
     6 
     7 from emp e
     8 
     9 where e.ename like'c%';
    10 
    11  
    12 
    13 -- 查询名字中第二个字母是M的雇员
    14 
    15 select e.*
    16 
    17 from emp e
    18 
    19 where e.ename like'_M%'
    20 
    21  
    22 
    23 -- 查询名字中含有M的雇员
    24 
    25 select e.*
    26 
    27 from emp e
    28 
    29 where e.ename like'%M%';
    30 
    31  
    32 
    33 -- 查询名字中含有%的雇员
    34 
    35 select e.*
    36 
    37 from emp e
    38 
    39 where e.ename like'%\%%'escape('');

    2 . 复杂查询(and/or)

    where后面的条件可以跟多个通过and 或者or 连接

    and:且、并且

    or: 或、或者

     1 -- 查询部门10且薪资大于等2000的雇员
     2 
     3 select e.*
     4 
     5 from emp e
     6 
     7 where e.deptno = 10and e.sal >= 2000;
     8 
     9  
    10 
    11 -- 查询名字中含M且薪资大于1000的雇员
    12 
    13 select e.*
    14 
    15 from emp e
    16 
    17 where e.ename like'%M%'and e.sal >1000
    18 
    19  
    20 
    21 -- 查询部门在10或20的雇员
    22 
    23 select e.*
    24 
    25 from emp e
    26 
    27 where e.deptno = 10or e.deptno = 20

    where中and、or的执行效率问题

     1 -- 思考:查询条件的顺序对查询速度是否有影响?
     2 
     3  
     4 
     5 /*
     6 
     7 分析:
     8 
     9 and 表示且,条件越多,检索的数据量越来越少
    10 
    11 or 表示或,条件越多,检索的数据量越来越多
    12 
    13 where 条件的执行顺序从后向前
    14 
    15 */
    16 
    17  
    18 
    19 -- 优化后的sql
    20 
    21 select e.*
    22 
    23 from emp e
    24 
    25 where e.sal>=2000and e.deptno = 10
    26 
    27 -- 结论
    28 
    29 -- AND:  把检索结果较少的条件放到后面
    30 
    31  
    32 
    33 -- 查部门10或30的雇员
    34 
    35 select e.*
    36 
    37 from emp e
    38 
    39 where e.deptno = 10or e.deptno = 30;

    and和or同时存在时,and先执行。

    综合案例

     1 --使用in查询部门名称为 SALES 和 RESEARCH 的雇员姓名、工资、部门编号
     2 
     3 -- 思考:部门名称位于dept,雇员信息位于emp表
     4 
     5 select e.ename,e.sal,e.deptno
     6 
     7 from emp e
     8 
     9 where e.deptno in
    10 
    11 (
    12 
    13 select d.deptno
    14 
    15 from dept d
    16 
    17 where d.dname = 'SALES'or d.dname = 'RESEARCH'
    18 
    19 );

    3.   计算字段

    我们经常需要把数据库中检索出来的信息进行再加工,允许的操作+、-、*、/。通过四个运算得到新的字段(计算字段)。

    计算字段在数据表中不存在。

    -- 查询出每个雇员的月薪(收入)
    
    select e.ename,e.sal+e.comm as "收入",e.deptno
    
    from emp e

    注意:很多记录中的comm是null,表示不确定的值,经常四则运算后的值也不确定。

    当遇到字段时null时,可以通过nvl函数把null转化便于运算的类型。

    -- nvl函数优化
    
    select e.ename,e.sal+nvl(e.comm,0) "收入",e.deptno
    
    from emp e
  • 相关阅读:
    Ajax传值以及接受传值,@ResPonseBody 和 @RequestBody
    分页
    延迟加载
    mybatis的一级缓存和二级缓存
    拦截器的使用
    Session和Cookie
    逆向工程
    springmvc注解详解
    Java——变量
    Go通关04:正确使用 array、slice 和 map!
  • 原文地址:https://www.cnblogs.com/qq2267711589/p/10859389.html
Copyright © 2020-2023  润新知