加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Oracle系列:(15)集合查询

发布时间:2020-12-12 16:00:34 所属栏目:百科 来源:网络整理
导读:使用并集运算,查询20号部门或30号部门的员工信息 select*fromempwheredeptno=20unionselect*fromempwheredeptno=30; 650) this.width=650;" src="http://img.jb51.cc/vcimg/static/loading.png" title="001.PNG" alt="wKiom1fQHJqCkdMGAAA6IAD2YSM221.png"

当多表查询,子查询,集合查询都能完成同样任务时,按如下优化方案选择:

多表查询->子查询->集合查询

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!


使用并集运算,查询20号部门或30号部门的员工信息

select*fromempwheredeptno=20
union
select*fromempwheredeptno=30;

wKiom1fQHJqCkdMGAAA6IAD2YSM221.png

注意:

union:二个集合中,如果都有相同的,取其一

union all:二个集合中,如果都有相同的,都取


使用set time/timing on,打开时间的开关

settimeon;
settimeoff;

wKioL1fQHbKg1UxHAAAO-IqOO78132.png


使用set time/timing off,关闭时间的开关

settimingon;
settimintoff;

wKiom1fQHgnzJP96AAAXTGalaG8288.png


使用交集运算[intersect],查询工资在1000-2000和1500-2500之间的员工信息(方式一)

select*fromempwheresalbetween1000and2000
intersect
select*fromempwheresalbetween1500and2500;

wKioL1fQHlSzdTaSAAAby0yS9dE587.png


用where行过滤,查询工资在1000-2000和1500-2500之间的员工信息(方式二)

select*
fromemp
where(salbetween1000and2000)and(salbetween1500and2500);


使用差集运算[minus],查询工资在1000-2000,但不在1500-2500之间的员工信息(方式一)

select*fromempwheresalbetween1000and2000
minus
select*fromempwheresalbetween1500and2500;

wKioL1fQHpvwgBI9AAAlb0Re2nY619.png


使用where行过滤,查询工资在1000-2000,但不在1500-2500之间的员工信息(方式二)

select*
fromemp
where(salbetween1000and2000)and(salnotbetween1500and2500);


集合查询的细节

1)集合操作时,必须确保集合列数是相等的

select empno,ename,sal,comm from emp where deptno = 20

union

select empno,sal from emp where deptno = 30;错


2)集合操作时,必须确保集合列类型对应相同

select empno,hiredate from emp where deptno = 30;错


3)A union B union C = C union B union A

select * from emp where deptno = 10

union

select * from emp where deptno = 20

union

select * from emp where deptno = 30;


4)当多个集合操作时,结果的列名由第一个集合列名决定

selectempno"编号",ename"姓名",sal"薪水"fromempwheredeptno=20
union
selectempno,salfromempwheredeptno=10;

wKioL1fQHyeRyj2yAAAix3UZ__A216.png


    推荐文章
      热点阅读