• postgresql-集合运算


    并集

    在这里插入图片描述

    create table excellent_emp(
    year int not null,
    emp_id integer not null,
    constraint pk_excellent_emp primary key(year,emp_id)
    );
    
    insert into excellent_emp values(2018,9);
    insert into excellent_emp values(2018,11);
    insert into excellent_emp values(2019,9);
    insert into excellent_emp values(2019,20);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    /*
     * union
     * distinct 将合并后的结果集进行去重;(数据量大的时候影响性能)
     * all 保留结果集中的重复记录
     * 默认是distinct
     * */
    select e.emp_id 
    from excellent_emp e
    where e."year" =2018
    union distinct 
    select 
    e.emp_id 
    from excellent_emp e
    where e."year" = 2019;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在这里插入图片描述

    交集

    在这里插入图片描述

    -- 2018和2019连续获得优秀员工称号的员工
    /*
     * intersect 返回两个查询结果中的共同部分,
     * 即同时出现在第一个查询结果和第二个查询结果中的数据
     * distinct 将合并后的结果集进行去重
     * all 保留结果集中的重复记录
     * */
    select
    	e.emp_id
    from
    	cps.public.excellent_emp e
    where
    	e."year" = 2018
    intersect distinct 
    select
    	e2.emp_id
    from
    	cps.public.excellent_emp e2
    where
    	e2."year" = 2019;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    在这里插入图片描述

    差集

    在这里插入图片描述

    /*
     * 2019年获得优秀员工称号的新晋优秀员工
     * except:返回出现在第一个查询结果中,但不在第二个查询结果中的数据
     * distinct 将合并后的结果集进行去重
     * all 保留结果集中的重复记录
     * 默认是distinct
     */
    select
    	e.emp_id
    from
    	excellent_emp e
    where
    	e."year" = 2019
    except 
    select
    	e2.emp_id
    from
    	excellent_emp e2
    where
    	e2."year" = 2018;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    在这里插入图片描述

    集合运算符的优先级

    -- 相同的运算符,前后按照顺序执行的
    select *
    from (values(1)) t1(n)
    union
    select * 
    from (values(1)) t2(n)
    union all
    select * from (values(1)) t3(n);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述

    -- 不同的运算符,intersect 的优先级高于 union 和 except
    select *
    from (values(1)) t1(n)
    union all
    select * 
    from (values(1)) t2(n)
    intersect
    select * from (values(1)) t3(n);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述

    -- 使用括号可以修改集合操作的执行顺序,先执行有括号的,再执行无括号
    (select *
    from (values(1)) t1(n)
    union all
    select * 
    from (values(1)) t2(n))
    intersect
    select * from (values(1)) t3(n);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述

  • 相关阅读:
    Elasticsearch(二)- 索引-分片过滤器与延迟再分配
    MHA高可用配置及故障切换
    2022年Java就业方向有哪些?
    netty学习 java-io
    根据format索引值返回括号中的参数
    免杀实战-EDR对抗
    数据结构--时间复杂度和空间复杂
    排序算法之-快速
    Linux CentOS 本地yum配置
    (附源码)springboot校园商铺系统 毕业设计 052145
  • 原文地址:https://blog.csdn.net/Java_Fly1/article/details/132729306