码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • postgresql-集合运算


    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

    在这里插入图片描述

  • 相关阅读:
    ASTM标准涵盖哪些产品类别?出口美国的产品做ASTM认证需要注意哪些事项?
    【软件测试及质量保证】小结
    【SQLite3】使用 SQLite 为模型训练中的并行 dataloader 节省内存
    【技术干货】如何通过 DP 实现支持经典蓝牙的联网单品设备与 App 配对
    计算机视觉 图像形成 几何图形和变换
    62. 不同路径
    【微服务的集成测试】python实现-附ChatGPT解析
    解决Python requests库中的重定向问题
    elementui table超出两行显示...鼠标已入tip显示
    R语言中实现马尔可夫链蒙特卡罗MCMC模型
  • 原文地址:https://blog.csdn.net/Java_Fly1/article/details/132729306
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号