首页 > 技术文章 > Oarcle之集合操作

weishenme 2019-04-19 22:31 原文

计算字段(列):不在于表中,通过x、÷操作和列进行计算得到的列;

 

获取员工的年薪

  select (ename || '的年薪为:' || sal*12) info from emp;

  *info 为表的名字,可以根据需求自己命名(中英文都可以);

 

集合:每次查询结果可以看做一个集合

  select * from emp where deptno = 20;

  select * from emp where sal >2000;

 

-----------------------------------------------------------

union 并集 

  *给定两个集合A,B,把他们所有的元素合并在一起组成的集合,叫做集合A与集合B的并集,记作A∪B,读作A并B。(来源百度)

 

  select * from emp where deptno = 20

  union

  select * from emp where sal > 2000;

-------------------------------------------------------------------

union all 全集

  *一般的,如果一个集合含有我们所研究问题中涉及的所有元素,那么就称这个集合为全集,通常记作U。(来源百度)

 

    select * from emp where deptno = 20

 

    union all

 

    select * from emp where sal > 2000;

 

  *union和union all的区别在于:union all会重复显示两个集合相同的部分;

---------------------------------------------------------------------------

intersect :交集

  select * from emp where deptno =20

  intersect

  select * from emp where sal >2000;

  *集合论中,设AB是两个集合,由所有属于集合A且属于集合B的元素所组成的集合,叫做集合A与集合B的交集(intersection),记作AB。(来源百度)

 

--------------------------------------------------------------------

 

差集

差集定义:一般地,设A,B是两个集合,由所有属于A且不属于B的元素组成的集合,叫做集合A减集合B(或集合A与集合B之差),类似地,对于集合A.B,我们把集合{x/x∈A,且x¢B}叫做A与B的差集,记作A-B记作A-B(或A\B),即A-B={x|x∈A,且x ¢B}(或A\B={x|x∈A,且x ¢B} B-A={x/x∈B且x¢A} 叫做B与A的差集
---------------------
(作者:光于前裕于后
 来源:CSDN
 原文:https://blog.csdn.net/dr_guo/article/details/51182626
 版权声明:本文为博主原创文章,转载请附上博文链接!)

 

-------------------  差集                                -------------------------------------------------------------------

 


select * from emp where deptno = 20

minus

select * from emp where sal >2000;

- - - - - - - - -或者- - - - - - -

select * from emp where sal>2000

minus 

select * from emp where deptno =20;

* 两种写法所得到的结果不一样。

*使用集合操作注意:两条sql语句必须保证查询的列是一致的

 

错误1:列数不匹配
select * from emp where sal > 2000
minus
select empno from emp where deptno = 20;

 

错误2:数据类型不匹配
select ename from emp where sal > 2000
minus
select empno from emp where deptno = 20;

 


错误3:该条sql无意义
select sal from emp where sal > 2000
minus
select empno from emp where deptno = 20;

 

 

 

 

 

推荐阅读