您的位置:首页 > 数据库

第7章 集合运算-SQL基础教程

2019-05-22 09:35 1781 查看

7-1 表的加减法

什么是集合运算

集合在数据库领域表示记录的集合(表、视图和查询的执行结果)
集合运算就是对满足同一规则的记录进行的加减等四则运算

表的加法——UNION(并集)

create table product2(
product_id char(4) primary key,
product_name varchar(100) not null,
product_type varchar(32) not null,
sale_price int,
purchase_price int,
regist_date date
);

insert into product2
values
('0001','T恤','衣服',1000,500,'2008-09-20'),
('0002','打孔器','办公用品',500,320,'2009-9-11'),
('0003','运动T恤','衣服',4000,2800,null),
('0009','手套','衣服',800,500,null),
('0010','水壶','厨房用具',2000,1700,'2009-09-20');

select *
from product2;
/*
使用UNION对表进行加法(并集)运算
集合运算符会除去重复的记录
*/
select product_id,product_name
from product
union
select product_id,product_name
from product2;

集合运算的注意事项

注意事项①——作为运算对象的记录的列数必须相同
注意事项②——作为运算对象的记录中列的类型必须一致
注意事项③——可以使用任何SELECT语句,但ORDER BY子句只能在最后使用一次

包含重复行的集合运算——ALL选项

select product_id,product_name
from product
union all
select product_id,product_name
from product2;

选取表中公共部分——INTERSECT(交集)

因为MySQL尚不支持INTERSECT,所以无法使用

select product_id,product_name
from product
intersect
select product_id,product_name
from product2
order by product_id;

与使用 AND 可以选取出一张表中满足多个条件的公共部分不同,INTERSECT 应用于两张表,选取出它们当中的公共记录

其注意事项与 UNION 相同,希望保留重复行时同样需要使用INTERSECT ALL

记录的减法——EXCEPT(差集)

MySQL还不支持EXCEPT,因此也无法使用

select product_id,product_name
from product
except
select product_id,product_name
from product2
order by product_id;

EXCEPT 有一点与 UNION 和 INTERSECT 不同,需要注意一下。那就是在减法运算中减数和被减数的位置不同,所得到的结果也不相同

7-2 联结(以列为单位对表进行联结)

什么是联结

联结(JOIN)就是将其他表中的列添加过来,进行“添加列”的集合运算
UNION是以行(纵向)为单位进行操作,而联结则是以**列(横向)**为单位进行的

内联结——INNER JOIN

select sp.shop_id,sp.shop_name,sp.product_id,p.product_name,p.sale_price
from shop_product sp
inner join product p
on sp.product_id = p.product_id;

内联结要点①——进行联结时需要在FROM子句中使用多张表
内联结要点②——进行内联结时必须使用ON子句,并且要书写在FROM和WHERE之间
内联结要点③——使用联结时SELECT子句中的列需要按照“<表的别名>.<列名>”的格式进行书写

-- 内联结和WHERE子句结合使用
select sp.shop_id,sp.shop_name,sp.product_id,p.product_name,p.sale_price
from shop_product sp
inner join product p
on sp.product_id = p.product_id
where sp.shop_id = '000A';

外联结——OUTER JOIN

外联结要点①——选取出单张表中全部的信息
外联结要点②——使用 LEFT 时 FROM 子句中写在左侧的表是主表,使用 RIGHT 时右侧的表是主表

3张以上的表的联结

create table inventory_product(
inventory_id char(4),
product_id char(4),
inventory_quantity int not null,
primary key(inventory_id, product_id)
);

insert into inventory_product
values
('P001', '0001', 0),
('P001', '0002', 120),
('P001', '0003', 200),
('P001', '0004', 3),
('P001', '0005', 0),
('P001', '0006', 99),
('P001', '0007', 999),
('P001', '0008', 200),
('P002', '0001', 10),
('P002', '0002', 25),
('P002', '0003', 34),
('P002', '0004', 19),
('P002', '0005', 99),
('P002', '0006', 0),
('P002', '0007', 0),
('P002', '0008', 18);

select *
from inventory_product;
-- 对3张表进行内联结
select sp.shop_id,sp.shop_name,sp.product_id,p.product_name,p.sale_price,ip.inventory_quantity
from shop_product sp
inner join product p
on sp.product_id = p.product_id
inner join inventory_product ip
on sp.product_id = ip.product_id
where ip.inventory_id = 'P001';

交叉联结——CROSS JOIN

/*
对满足相同规则的表进行交叉联结的集合运算符是 CROSS JOIN(笛卡儿积)
交叉联结是对两张表中的全部记录进行交叉组合,因此结果中的记录数通常是两张表中行数的乘积
*/
select sp.shop_id,sp.shop_name,sp.product_id,p.product_name
from shop_product sp
cross join product p;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: