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 保留结果集中的重复记录
 * */
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;

在这里插入图片描述

差集

在这里插入图片描述

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

在这里插入图片描述

集合运算符的优先级

-- 相同的运算符,前后按照顺序执行的
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);

在这里插入图片描述

PostgreSQL是以加州大学伯克利分校计算机系开发的POSTGRES,现在已经更名为PostgreSQL. PostgreSQL支持大部分SQL标准并且提供了许多其它现代特性:复杂查询、外键、触发器、视图、事务完整性等。 PostgreSQL 是一个免费的对象-关系数据库服务器(数据库管理系统),它在灵活的 BSD-风格许可证下发行。它提供了相对其他开放源代码数据库系统(比如 MySQL 和 Firebird),和专有系统(比如 Oracle、Sybase、IBM 的 DB2 和 Microsoft SQL Server)之外的另一种选择。 事实上, PostgreSQL 的特性覆盖了 SQL-2/SQL-92 和 SQL-3/SQL-99,首先,它包括了可以说是目前世界上最丰富的数据类型的支持,其中有些数据类型可以说连商业数据库都不具备, 比如 IP 类型和几何类型等;其次,PostgreSQL 是全功能的自由软件数据库,很长时间以来,PostgreSQL 是唯一支持事务、子查询、多版本并行控制系统(MVCC)、数据完整性检查等特性的唯一的一种自由软件的数据库管理系统。 Inprise 的 InterBase 以及SAP等厂商将其原先专有软件开放为自由软件之后才打破了这个唯一。最后,PostgreSQL拥有一支非常活跃的开发队伍,而且在许多黑客的努力下,PostgreSQL 的质量日益提高。从技术角度来讲,PostgreSQL 采用的是比较经典的C/S(client/server)结构,也就是一个客户端对应一个服务器端守护进程的模式,这个守护进程分析客户端来的查询请求,生成规划树,进行数据检索并最终把结果格式化输出后返回给客户端。为了便于客户端的程序的编写,由数据库服务器提供了统一的客户端 C 接口。而不同的客户端接口都是源自这个 C 接口,比如ODBC,JDBC,Python,Perl,Tcl,C/C++,ESQL等, 同时也要指出的是,PostgreSQL 对接口的支持也是非常丰富的,几乎支持所有类型的数据库客户端接口。这一点也可以说是 PostgreSQL 一大优点。 本课程作为PostgreSQL数据库管理一,主要讲解以下内容:1.     PostgreSQL安装和环境准备2.     PostgreSQL数据查询3.     PostgreSQL 数据过滤4.     PostgreSQL 多表的联接5.     PostgreSQL数据的分组6.     PostgreSQL合集的操作7.   PostgreSQL 合集的分组
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值