oracle --union和union all

当咱们须要将两个select语句的结果做为一个总体显示时,就须要使用到union或者union all关键字。sql

union的做用是将多个结果合并在一块儿显示出来。ui

union和uinon all的区别是:union会对结果集中的重复结果去重,而union all则会将全部的结果所有显示出来。spa

union:对两个(或多个)结果集进行并集操做,不包括重复行,同时进行默认规则的排序。code

union all:对两个结果集进行并集操做,包括重复行,不进行排序。排序

能够在最后一个结果集中指定order by子句改变排序方式。
io

例子以下
class

用户表t_user以下select

经销商表t_fchs以下nio

union链接两张表,只显示两个字段:名称和电话号码im

select
*
from
(select
tu.user_name as name,tu.telephone
from
t_user tu)
union
(select
tf.fchs_name,tf.fchs_telephone
from
t_fchs tf)

结果以下:

union all链接两张表

select
*
from
(select
tu.user_name as name,tu.telephone
from
t_user tu)
union all
(select
tf.fchs_name,tf.fchs_telephone
from
t_fchs tf)

结果以下:

由上可知:union是去重了的(去掉了 name:米乐 的那一行),union all所有显示出来。

2、intersect和minus的用法

Intersect:对两个结果集进行交集操做,不包括重复行。默认规则排序

例如:对t_user表和t_fchs表求交集

select
*
from
(select
tu.user_name as name,tu.telephone
from
t_user tu)
Intersect
(select
tf.fchs_name,tf.fchs_telephone
from
t_fchs tf)

结果以下:

备注:由两张表可知,交集的结果就只有这一个。

3、Minus的用法

Minus:对两个结果集进行差操做,不包括重复行,同时默认排序

minus的做用是去同留异

select
*
from
(select
tu.user_name as name,tu.telephone
from
t_user tu)
minus
(select
tf.fchs_name,tf.fchs_telephone
from
t_fchs tf)

结果以下:

备注:t_user与t_fchs的差集操做获得的结果是

t_user中与t_fchs表中相同的去掉了,不一样的(只针对t_user表)留下来了。

相关文章
相关标签/搜索