博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Oracle - 查询
阅读量:4565 次
发布时间:2019-06-08

本文共 3359 字,大约阅读时间需要 11 分钟。

1 查询语句

  • 查看账户下的所有表
1 select * from tab;
  • 查看账户下的所有表的详细信息
1 select * from user_tables;

1.1 select

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

1 select field1,filed2,.. .2 from tablename3 [where condition];

利用 Oracle 数据库 Scott 账户下的 EMP 表进行练习

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

1.2 distinct 去重

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

1 -- 查询公司的工种2 select distinct e.job3 from emp e;

可以修饰多字段,多个字段的值都一样的记录才去掉。

1.3 where 字句

where 表示查询的条件。

  • =,!=,<>,<,>,<=,>= 关系运算符
<> 表示不等于
1 -- 把部分10的雇员查询出来 2 select *  3 from emp  4 where deptno = 10; 5  6 -- 把名称为smith的雇员 7 select e.* 8 from emp e 9 where e.ename = 'SMITH';10 11 -- 查询底薪大于等于1000的员工12 select e.*13 from emp e14 where e.sal >= 1000;15 16 select e.*17 from emp e18 where e.sal <> 800
  • any/some/all (list)

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

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

1 -- 查询薪资大于 800 的雇员2 select e.*3 from emp e4 where e.sal > some(1000,800);5 6 -- 查询薪资大于 10007 select e.*8 from emp e9 where e.sal > all(1000,800);
  • is null/is not null

null 在 SQL 中表示的是不确定

1 -- 查询没有津贴的雇员2 select e.*3 from emp e4 where e.comm is null;5 6 -- 查询有津贴的雇员7 select e.*8 from emp e9 where e.comm is not null;
  • between x and y

表示一个值位于[x,y]区间,x/y 可以是数字或字符串。

字符串小写字母大于大写字母;逐位比字母,字母相同再比长度。
1 -- 查询薪资在1000-5000之间的雇员2 select e.*3 from emp e4 where e.sal between 1000 and 5000;
  • in/not in (list)

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

1 -- 查询部门号是10和20的员工 2 select e.* 3 from emp e 4 where e.deptno in(10,20); 5  6 -- 查询部门号不是10和20的员工 7 select e.* 8 from emp e 9 where e.deptno not in(10,20);10 11 -- 查询薪资是1000,2000,5000的员工12 select e.*13 from emp e14 where e.sal in (1000,2000,5000);
  • 模糊查询

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

  1. %:表示任意字符出现多次(含0次),
  2. _:表示任意字符出现1次。
  3. 当需要查询的格式包含关键字,需要用转义字符。escape(‘x’) 表示指定转义字符为x,一般指定为\
1 -- 查询名字是c开头的雇员 2 select e.* 3 from emp e 4 where e.ename like 'c%'; 5  6 -- 查询名字中第二个字母是M的雇员 7 select e.* 8 from emp e 9 where e.ename like '_M%'10 11 -- 查询名字中含有M的雇员12 select e.*13 from emp e14 where e.ename like '%M%';15 16 -- 查询名字中含有%的雇员17 select e.*18 from emp e19 where e.ename like '%\%%' escape('\');

2 复杂查询(and/or)

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

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

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

  • and 中,让结果数据量少的先执行
  • or 中,让结果数据量多的先执行
  • where 中,条件的执行顺序从后向前
  • and 和 or 同时存在时,and 先执行

综合案例

根据部门名称,查询雇员信息。部门名称存在于 DEPT 表中,其中的 DEPTNO 和 雇员表 EMP 中的 DEPTNO 对应。

1 select e.ename,e.sal,e.deptno2 from emp e3 where e.deptno in 4 (5 select d.deptno6 from dept d7 where d.dname = 'SALES' or d.dname = 'RESEARCH'8 );

3 计算字段

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

1 -- 查询出每个雇员的月薪(收入)2 select e.ename,e.sal+e.comm as "收入",e.deptno3 from emp e;
这里要注意,表中部分记录的 comm 字段为空,null + 具体数字的结果依然是 null。可以通过 nvl 函数把 null 转换成具体数值方便计算。
nvl(a , b)-若 a 为 null,则函数值为 b;若 a 不为 null,则函数值为 a。
1 -- nvl函数优化2 select e.ename,e.sal+nvl(e.comm,0) "收入",e.deptno3 from emp e;

 

转载于:https://www.cnblogs.com/carlosouyang/p/10859019.html

你可能感兴趣的文章
ubuntu16.04 配置爬虫环境
查看>>
Centos7,PHP7安装swoole
查看>>
02_ListActive中响应事件 并LogCat输出
查看>>
doubleclick adx note
查看>>
Celery框架
查看>>
[c#]asp.net开发微信公众平台(4)关注事件、用户记录、回复文本消息
查看>>
[转载,感觉写的非常详细]DUBBO配置方式详解
查看>>
linux Valgrind使用说明-内存泄漏
查看>>
Android在Eclipse上的环境配置
查看>>
面向对象(五)
查看>>
android平台下使用点九PNG技术
查看>>
Python学习3,列表
查看>>
最长回文子串
查看>>
JAVA基础-JDBC(一)
查看>>
js中for和while运行速度比较
查看>>
简单理解什么是递归(阶乘演示)
查看>>
http协议
查看>>
js倒计时,页面刷新时,不会从头计时
查看>>
洛谷1156垃圾陷阱
查看>>
python ==》 递归
查看>>