Oracle:集合运算

来源:互联网 发布:看美剧用什么视频软件 编辑:程序博客网 时间:2024/06/09 21:05

注意的问题:

(1)参与运算的各个集合必须列数相同,且类型一致。

(2)采用第一个集合的表头作为最后的表头。

(3)如果排序,必须在每个集合后使用相同的orderby。

(4)括号的问题,可以使用括号,先执行后面的语句。

 

--查询部门号是1020的员工信息

--方式一:select* from emp where deptno in (10,20);

--方式二:select*from emp where deptno=10 or deptno=20;

--集合运算方式:并集(union),交集(INTERSECT),差集(minus)

select * from emp where deptno=10 unionselect * from emp where deptno=20;

/*

关于第一个注意:之前的制表案例中,rollup语句等于3条语句相加,可以使用union完成,但是要列数且类型一样。

对于缺少的列,要自己手动建立列,使之满足条件:

*/

Select deptno,job,sum(sal) from emp groupby deptno,job

Union

Select deptno,to_char(null),sum(sal)from emp group by deptno

Union

Select to_number(null),to_char(null),sum(sal)from emp;

 

--set timing on ,可以开启时间统计,可以用于测试sql的执行时间 set timing off关闭。

 

--交集intersect

select ename,sal from emp where sal between700 and 1300

intersect

select ename,sal from emp where sal between1201 and 1400;

 

--差集:显示薪水同时位于级别1(700-1300),但不属于级别2(1201-1400)的员工信息。属于上边但不属于下边

Select ename,sal from emp where sal between700 and 1300

Minus

Selectename,sal from emp where sal between 1201 and 1400;
原创粉丝点击