• 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

    在这里插入图片描述

  • 相关阅读:
    拷贝构造函数(深拷贝+浅拷贝)
    设计模式之组合模式
    leetcode(力扣)135. 分发糖果 (贪心)
    卷绕工艺与叠片工艺的对比
    CodeTON Round 3 (Div. 1 + Div. 2, Rated, Prizes!) A. Indirect Sort 解题报告
    这是我见过史上最强的spring全家桶笔记,在GitHub上两天破百万瞬间爆火
    【趣味随笔】盘点那些国内外知名的扫地机器人品牌
    复现XSS漏洞及分析
    linux安装配置 kafka并简单使用
    【C++】二叉搜索树
  • 原文地址:https://blog.csdn.net/Java_Fly1/article/details/132729306