-- 查询员工表中部门编号设计select EMPNO,ENAME,JOB from emp where DEPTNO =10;-- distinct 去重查询 必须放到开头,多个字段的时候,每个字段都不一样才去重selectdistinct DEPTNO from emp;-- 查询过程中可以个列添加别名select e.EMPNO 雇员编号,e.ENAME as 雇员名称,e.JOB 雇员工作 from EMP e where e.DEPTNO =10;--查询所有的字段可以使用*;但是在项目中不要使用select*from EMP;/*
=,!=,<>,<,>,<=,>=,any,some,all
is null,is not null
between x and y
in(list),not in(list)
exists(sub-query)
like _ ,%,escape ‘\‘ _\% escape ‘\’
*/-- =select*from EMP where DEPTNO =20;-- !=select*from EMP where DEPTNO !=20;-- <> 不等于select*from EMP where DEPTNO <>20;-- <select*from EMP where SAL <1500;-- >select*from EMP where SAL >1500;-- <=select*from EMP where SAL <=1500;-- >=select*from EMP where SAL >=1500;-- any 其中的任意一个select*from EMP where SAL >any(1000,1500,3000);-- some 与any效果一样 只要其中的一个条件成立即可select*from EMP where SAL >some(1000,1500,3000);-- all 大于所有的值都会成立select*from EMP where SAL >all(1000,1500,3000);-- is null;null在sql中有个特殊含义 null != null不能使用,不能使用!= ; = 判断,需要使用is is not;select*from EMP where comm isnull;-- is not nullselect*from EMP where COMM isnotnull;select*from EMP wherenullisnull;-- between x and y,包含 x y;select*from EMP where SAL between1500and3000;select*from EMP where SAL>=1500and SAL <=3000;-- 对一些值 进行等值判断可以使用 in 和 not in-- in(list)select*from EMP where DEPTNO in(10,20);-- and和or者这样的关键字,and相当与操作,or相当与或操作-- and和or可能出现在同一个sql中,需要注意and和or的优先级-- and的优先级高与于or的优先级,or的操作需要使用括号来保证优先级select*from EMP where DEPTNO =10or DEPTNO =20;-- not in(list)select*from EMP where DEPTNO notin(10,20);select*from EMP where DEPTNO !=10and DEPTNO !=20;/*
exists (sub-query),当exists中的子查询语句,能查到对应结果的时候,
意味着条件满足
相当于双层for循环,
*/-- 查询部门内编号为10和20的员工,要求使用exists实现select*from emp where DEPTNO =10or DEPTNO =20;select*from EMP e
whereexists(select d.DEPTNO from DEPT d where(d.DEPTNO =10or d.DEPTNO =20)and e.DEPTNO = d.DEPTNO);/*
模糊查询:
like _ ,%,escape ‘\‘ _\% escape ‘\’
在like的语句中,需要使用占位符或者通配符
_,某个字符或者数字仅出现一次
%,任意字符出现任意次数
escape,使用转义字符,可以自己规定转义字符
使用like的时候要慎重,因为like的效率比较低
使用like可以参考使用索引,但是要求不能以%开头
涉及到大文本的检索的时候,可以使用某些框架 luence,solr,elastic search
*/-- 查询s开头的的用户select*from EMP where ENAME like('S%');-- 查询名字以S开头,且倒数第二个字为Tselect*from EMP where ENAME like('S%T_');-- 查询名字中带有%的用户select*from EMP where ENAME like('%\%%')escape('\');
/*
order by进行排序操作
默认情况下完成的是升序的操作,
asc:是默认的排序方式,表示升序
desc:降序的排序方式
排序是按照自然顺序进行排序的
如果是数值,那么按照从大到小
如果是字符串,那么按照字典序排序
在进行排序的时候可以指定多个字段,而且多个字段可以使用不同的排序方式
每次在执行order by的时候相当于是做了全排序,思考全排序的效率
会比较耗费系统的资源,因此选择在业务不太繁忙的时候进行
*/
select *
from EMP order by SAL;
select *
from EMP order by SAL desc;
select *
from emp order by ENAME;
select *
from EMP ORDER BY SAL desc ,ENAME asc ;
-- 使用计算字段
-- 字符串连接
select 'my name is'||ENAME from emp;
select concat('my name is ',ENAME) 姓名
from EMP;-- 计算所有员工的年薪select ENAME,(e.SAL+COMM)*12from EMP e;/*
null是比价特殊的存在;null做任何计算都是null,因此要进行非空判断
引入nvl,nvl(a1,a2),如果是a1为空的,则返回a2;如果不是空则返回a1
*/select e.ENAME,(e.SAL+nvl(e.COMM,0))*12 年薪
from EMP e;-- dual是oracle中的虚拟表,没有实际的数据,可以用来测试select100+nullfrom DUAL;-- 交集 并集 差集计算-- Aselect*from EMP where DEPTNO =30;-- Bselect*from EMP where SAL >3000;-- 并集: 将两个表中所有的数据显示出来,但是没有重复的数据只显示一次select*from EMP where DEPTNO =30unionselect*from EMP where SAL >1000;-- 全集: 两个表全部数据,不进行去重操作select*from EMP where DEPTNO =30unionallselect*from EMP where SAL >1000;-- 交集: 两个集合中交叉显示的数据集,只显示一次select*from EMP where DEPTNO =30intersectselect*from EMP where SAL >1000;-- 差集: 包含集合A集合而不包含集合B的数据,跟A和B的顺序相关select*from EMP where DEPTNO =30 minus
select*from EMP where SAL >1000;