当前位置: 首页 > news >正文

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);
/** 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;

在这里插入图片描述

交集

在这里插入图片描述

-- 2018和2019连续获得优秀员工称号的员工
/** intersect 返回两个查询结果中的共同部分,* 即同时出现在第一个查询结果和第二个查询结果中的数据* distinct 将合并后的结果集进行去重* all 保留结果集中的重复记录* */
selecte.emp_id
fromcps.public.excellent_emp e
wheree."year" = 2018
intersect distinct 
selecte2.emp_id
fromcps.public.excellent_emp e2
wheree2."year" = 2019;

在这里插入图片描述

差集

在这里插入图片描述

/** 2019年获得优秀员工称号的新晋优秀员工* except:返回出现在第一个查询结果中,但不在第二个查询结果中的数据* distinct 将合并后的结果集进行去重* all 保留结果集中的重复记录* 默认是distinct*/
selecte.emp_id
fromexcellent_emp e
wheree."year" = 2019
except 
selecte2.emp_id
fromexcellent_emp e2
wheree2."year" = 2018;

在这里插入图片描述

集合运算符的优先级

-- 相同的运算符,前后按照顺序执行的
select *
from (values(1)) t1(n)
union
select * 
from (values(1)) t2(n)
union all
select * from (values(1)) t3(n);

在这里插入图片描述

-- 不同的运算符,intersect 的优先级高于 union 和 except
select *
from (values(1)) t1(n)
union all
select * 
from (values(1)) t2(n)
intersect
select * from (values(1)) t3(n);

在这里插入图片描述

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

在这里插入图片描述

http://www.lryc.cn/news/158364.html

相关文章:

  • [持续更新]计算机经典面试题基础篇Day2
  • C++:类和对象(二)
  • Java“牵手”京东商品详情数据,京东商品详情API接口,京东API接口申请指南
  • Fluidd摄像头公网无法正常显示修复一例
  • 【C++ 学习 ⑳】- 详解二叉搜索树
  • Java中网络的基本介绍。网络通信,网络,ip地址,域名,端口,网络通信协议,TCP/IP传输过程,网络通信协议模型,TCP协议,UDP协议
  • 【Qt】总体把握文本编码问题
  • Linux命令(77)之curl
  • 详解 sudo usermod -aG docker majn
  • 大数据课程L2——网站流量项目的算法分析数据处理
  • jar包或exe程序设置为windows服务
  • 数据结构--- 树
  • 两个pdf文件合并为一个怎么操作?分享pdf合并操作步骤
  • Zookeeper简述
  • 1、Flutter移动端App实战教程【环境配置、模拟器配置】
  • stride与padding对输出尺寸的计算
  • Excel VSTO开发2 -建立Excel VSTO项目
  • chrome插件:一个基于webpack + react的chrome 插件项目模板
  • Vue:组件缓存
  • 【C++】DICOM医学影像工作站PACS源码
  • UDP的可靠性传输2
  • 《Java程序设计》实验报告
  • 数据可视化、BI和数字孪生软件:用途和特点对比
  • Ros noetic 机器人坐标记录运动路径和发布 实战教程(C)
  • Linux入门之多线程|线程的同步|生产消费模型
  • MATLAB解析和保存ini文件
  • 模型压缩-对模型结构进行优化
  • 软件工程课件
  • 基于ADS的marx雪崩电路设计-设计实践(射频脉冲源)
  • X86_64函数调用汇编程序分析