Mysql数据库的交、差运算

Mysql数据库的交、差运算

1、操作表section

2、创建中间表

1、创建fall表

create table fall select * from section where semester = Fall and year = 2017;

2、创建spring表

create table spring select * from section where semester = Spring and year = 2018;

3、进行交差运算

1、进行交运算

select course_id from spring inner join fall using(course_id);
//查找既在2017年秋季又在2018年春季开设的课程
select course_id from fall where course_id in(select course_id from spring);
+-----------+
| course_id |
+-----------+
| CS-101    |
+-----------+

+-----------+
| course_id |
+-----------+
| CS-101    |
+-----------+

2、进行差运算

select course_id from spring left join fall using(course_id) where fall.course_id is null;
//查询在2018年春季开设单不在2017年秋季开设的课程;
select course_id from spring where course_id not in(select course_id from fall);
+-----------+
| course_id |
+-----------+
| CS-315    |
| CS-319    |
| CS-319    |
| FIN-201   |
| HIS-351   |
| MU-199    |
+-----------+
经验分享 程序员 微信小程序 职场和发展