经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Oracle » 查看文章
Select 查询语句
来源:cnblogs  作者:曾经有一首歌  时间:2019/5/16 9:14:39  对本文有异议

1.1  查询语句

1.1.1     select

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

  1. select field1,filed2,.. .
  2. from tablename
  3. [where condition]

 

  1. 1 -- 查询所有员工的名字和雇员号
  2. 2 select empno,ename from emp;
  3. 3
  4. 4 -- 查询所有员工的雇员号、姓名、岗位
  5. 5 select empno,ename,job from emp;
  6. 6
  7. 7 -- 字段的别名 as
  8. 8 select ename as "姓名" from emp;
  9. 9 select ename as "姓名",job as "岗位" from emp;
  10. 10
  11. 11 -- 别名一定要用双引号,不能用单引号
  12. 12 select ename "姓名",job "岗位" from emp;
  13. 13 -- 双引号可以省略
  14. 14 select ename 姓名 from emp;
  15. 15
  16. 16 -- 表的别名
  17. 17 select emp.ename,emp.job from emp;
  18. 18 select e.ename,e.job from emp e;

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

1.1.2 distinct 去重

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

 

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

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

1.1.3     where 子句

where 表示查询的条件。

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

<> 表示不等于

  1. 1 -- where 子句
  2. 2
  3. 3 -- 把部分10的雇员查询出来
  4. 4 select *
  5. 5 from emp
  6. 6 where deptno = 10;
  7. 7
  8. 8 -- 把名称为smith的雇员
  9. 9 select e.*
  10. 10 from emp e
  11. 11 where e.ename = 'SMITH';
  12. 12
  13. 13 -- 查询底薪大于等于1000的员工
  14. 14 select e.*
  15. 15 from emp e
  16. 16 where e.sal >= 1000;
  17. 17
  18. 18 select e.*
  19. 19 from emp e
  20. 20 where e.sal <> 800

any/som/all(list)

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

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

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

 [2] null

null 在sql中表示的是不确定 => 可以认为没有值(一些情况下)

  1. 1 -- null/not null
  2. 2 -- 查询没有津贴的雇员
  3. 3 select e.*
  4. 4 from emp e
  5. 5 where e.comm is null
  6. 6
  7. 7 select e.*
  8. 8 from emp e
  9. 9 where e.comm is not null

【3】betweem x and y

表示一个值位于【x,y】区间,x与Y一般都是数字

  1. -- between x and y
  2. -- 查询薪资在1000-5000之间的雇员
  3. select e.*
  4. from emp e
  5. where e.sal between 1000 and 5000
  6.  
  7. -- 查询薪资在(3000,5000]之间的雇员
  8. select e.*
  9. from emp e
  10. where e.sal between 3000.01 and 5000

【4】in/not in list

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

  1. 1 -- in/not inlist
  2. 2 -- 查询部分号是1020的员工
  3. 3 select e.*
  4. 4 from emp e
  5. 5 where e.deptno in(10,20);
  6. 6
  7. 7 select e.*
  8. 8 from emp e
  9. 9 where e.deptno not in(10,20);
  10. 10
  11. 11 -- 查询薪资是1000,2000,5000的员工
  12. 12 select e.*
  13. 13 from emp e
  14. 14 where e.sal in (1000,2000,5000);

【5】模糊查询

like 关键字 用于模糊查询中

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

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

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

  1. -- 查询名字是c开头的雇员
  2.  
  3. select e.*
  4. from emp e
  5. where e.ename like 'c%';
  6. -- 查询名字中第二个字母是M的雇员
  7. select e.*
  8. from emp e
  9. where e.ename like '_M%'
  10.  
  11. -- 查询名字中含有M的雇员
  12. select e.*
  13. from emp e
  14. where e.ename like '%M%';
  15. -- 查询名字中含有%的雇员
  16. select e.*
  17. from emp e
  18. where e.ename like '%\%%' escape('\');

1.2 复杂查询(and /or)

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

and:且、并且

or:或、或者

  1. 1 -- 查询部门10且薪资大于等2000的雇员
  2. 2 select e.*
  3. 3 from emp e
  4. 4 where e.deptno = 10 and e.sal >= 2000;
  5. 5
  6. 6 -- 查询名字中含M且薪资大于1000的雇员
  7. 7 select e.*
  8. 8 from emp e
  9. 9 where e.ename like '%M%' and e.sal > 1000
  10. 10
  11. 11 -- 查询部门在1020的雇员
  12. 12 select e.*
  13. 13 from emp e
  14. 14 where e.deptno = 10 or e.deptno = 20

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

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

综合案例

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

1.3  计算字段

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

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

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

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

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

  1. -- nvl函数优化
  2. select e.ename,e.sal+nvl(e.comm,0) "收入",e.deptno
  3. from emp e


原文链接:http://www.cnblogs.com/ruckly/p/10872256.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号