oracle里面Union和minus的问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了oracle里面Union和minus的问题相关的知识,希望对你有一定的参考价值。
A集合1,2,3,4,5,
B集合4,5,6,7,8,
select * from A minus select * from B =1,2,3,6,7,8
select * from A union select * from B =1,2,3,4,5,6,7,8对吗?
也就是说minus是AB交集的相反集合,union是取AB的全集,如果取AB的交集怎么写?
这两个表结构完全一样,就是数据不一样
你下面的想法是错误的
select * from A minus select * from B =1,2,3,6,7,8
*****************************
SQL> select * from test001;
NAME
----------
1
2
3
4
5
SQL> select * from test002;
NAME
----------
4
5
6
7
8
SQL> select * from test001 minus select * from test002;
NAME
----------
1
2
3
SQL> select * from test001 union select * from test002;
NAME
----------
1
2
3
4
5
6
7
8
按照上面的结果,查询ab交集只需要
select * from A minus (select * from A minus select * from B);
SQL> select * from test001 minus (select * from test001 minus select * from test002);
NAME
----------
4
5本回答被提问者采纳 参考技术B 我不大懂你的意思,不过在数据库里,minus,和union要求上下两个集合类型相同而且,列名与上边查询一致,而且如果上下集合列明匹配不上,那么要用那to_char或to_number等函数,使类型一致,如Write a compound query that lists the following:
• Last names and department ID of all the employees from the EMPLOYEES table, regardless of
whether or not they belong to any department or not
• Department ID and department name of all the departments from the DEPARTMENTS table,
regardless of whether or not they have employees working in them
做法:select last_name,department_id, to_char(null)
from employees
union
select to_char(null),department_id,
department_name
from departments; 参考技术C select * from A intersect select * from B;
Intersect.Union,Union All,Intersect,Minus区别
Intersect和Minus的操作和Union基本一致,这里一起总结一下:
Union,对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序;
Union All,对两个结果集进行并集操作,包括重复行,不进行排序;
Intersect,对两个结果集进行交集操作,不包括重复行,同时进行默认规则的排序;
Minus,对两个结果集进行差操作,不包括重复行,同时进行默认规则的排序。
可以在最后一个结果集中指定Order by子句改变排序方式。
以上是关于oracle里面Union和minus的问题的主要内容,如果未能解决你的问题,请参考以下文章