SQL中intersect、union、minus和except 运算符

1、intersect运算符
intersect运算符通过只包括 TABLE1 和 TABLE2 中都有的行并消除所有重复行而派生出一个结果表。当 ALL 随 INTERSECT 一起使用时 (intersect  all),不消除重复行。
2、minus运算符
minus运算符通过只包括 TABLE1 有而 TABLE2 没有的行并消除所有重复行而派生出一个结果表。当 all随 minus一起使用时 (minus all),不消除重复行。
3、union运算符

.这三个关键字主要是对数据库的查询结果进行操作

 

union运算符是将两个或更多查询的结果组合为单个结果集 


table1:
f_name  f_date
name1   2009-6-1
name2   2009-6-2

table2:
f_name  f_date
name3   2009-6-2
name4   2009-6-3

select f_date from table1 intersect select date from table2
结果:
name2   2009-6-2


select f_date from table1 union select f_date from table2
结果:

2009-6-1
2009-6-2
2009-6-3

注:except
只能用于SQLserver
在SQLserver中:
select f_date from table1 except select f_date from table2
结果:
2009-6-1
在oracle中用minus:
select f_date from table1 minus select f_date from table2
结果:
2009-6-1

 

并集 :union: select × from a union (all) select × from b  》》》》aUb 
交集: intersect: select × from a intersect select × from b  》》》》a n b 
差集: minus: select × from a minus select × from b    》》》》a - b

posted @ 2019-09-29 15:09  孤寂!  阅读(1735)  评论(0)    收藏  举报