SQL中浅谈union、union all、Intersect、Minus的区别

今天我在工作中遇到这样一个查询语句:

select
    *   
from
    ( 

select
        id,
        EMPID,
        empname,
        departname,
        startdate,
        ENDDATE,
        null as palytime,
        '调休' as type          
    from
        TOS_CHECKING_TAKEDAY                
    union
    (
        select
            id,
            EMPID,
            empname,
            departname,
            null as startdate,
            null as enddate,
            palytime,
            '补单' as type                      
        from
            TOS_CHECKING_REPAIR          
    )   
union
(
    select
        id,
        EMPID,
        empname,
        departname,
        STATEDATE,
        enddate,
        null as palytime,
        '请假' as type              
    from
        VOS_VACATE_LABOUR   
)   
union
(
select
    id,
    TRAVELEMPID as EMPID,
    TRAVELEMP as empname,
    departname,
    ACTSTARTDATE as STATEDATE,
    ACTENDDATE enddate,
    null as palytime,
    '出差' as type       
from
    vos_travelreport   
)   

)   

where
1=1    

现在,就简单介绍一下联合语句。如果我们需要将两个或多个select语句的结果作为一个整体显示出来,这是我们就需要用到union或者union all关键字。union和union all的区别是,union会自动压缩多个结果集合中的重复结果,而union all则将所有的结果全部显示出来,不管是不是重复。

Intersect:对两个结果集进行交集操作,不包括重复行,重复的会被过滤,同时进行默认规则的排序。

Minus:对两个结果集进行差操作,返回的总是左边表中的数据且不包括重复行,重复的会被过滤,同时进行默认规则的排序。

值得注意的是:当我们要使用这两个联合SQL语句是,selecte查询语句的字段个数必须一样且字段类型要一致。在MySQL中不支持Intersect和Minus,但可以用简单的方法来代替。

来看下列:表scfrd_type

id         code

1             A

2             B


表scfrd_type1

id         code

2             B

3             C


查询语句select id,code fromscfrd_type  union select id,code from scfrd_type1。结果过滤了重复的行,如下:

id         code

1             A

2             B

3             C

查询语句select id,code fromscfrd_type  union  all select id,code from scfrd_type1。结果没有过滤了重复的行,如下:

id         code

1             A

2             B

2             B

3             C

查询语句select id,code fromscfrd_type  intersect select id,code from scfrd_type1。结果如下:

id         code

2             B

查询语句select id,code fromscfrd_type minus select id,code from scfrd_type1。结果如下:

id         code

1             A

sql相交和差写法分别如下:

1)select b.id,b.code from scfrd_type b left jion scfrd_type1 c on b.id=c.id and b.code=c.code where c.id is not null and c.code is not null;

或者select b.id,b.text from scfrd_type b INNER JOIN scfrd_type1 c using(id,text);

using(a,b)代表两个表的a,b字段分别相等;

2)select b.id,b.code from scfrd_type b left jion scfrd_type1 c on b.id=c.id and b.code=c.code where c.id is null and c.code is  null;




  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 PL/SQL UNIONUNION ALL、MINUS 和 INTERSECT 是用于合并和比较结果集的操作符。它们之间的区别如下: 1. UNIONUNION 操作符用于合并两个或多个 SELECT 语句的结果集,并去除重复的行。它返回一个包含所有唯一行的结果集。 2. UNION ALL:UNION ALL 操作符也用于合并两个或多个 SELECT 语句的结果集,但不去除重复的行。它返回一个包含所有行的结果集,包括重复的行。 3. MINUS:MINUS 操作符用于从第一个 SELECT 语句的结果集减去第二个 SELECT 语句的结果集,返回在第一个结果集存在但不在第二个结果集的行。它会去除重复的行。 4. INTERSECTINTERSECT 操作符用于返回同时存在于两个 SELECT 语句结果集的行,即交集。它会去除重复的行。 需要注意的是,这些操作符在比较结果集时要求两个 SELECT 语句具有相同的列数和相同或兼容的数据类型。 下面是一个示例: ``` SELECT column1 FROM table1 UNION SELECT column1 FROM table2; ``` 这将返回两个表 column1 列的唯一值。 ``` SELECT column1 FROM table1 UNION ALL SELECT column1 FROM table2; ``` 这将返回两个表 column1 列的所有值,包括重复的值。 ``` SELECT column1 FROM table1 MINUS SELECT column1 FROM table2; ``` 这将返回只在 table1 存在但不在 table2 存在的行。 ``` SELECT column1 FROM table1 INTERSECT SELECT column1 FROM table2; ``` 这将返回同时在 table1 和 table2 存在的行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值