相关文章
- sql 的 join、left join、full join的区别图解总结,测试,注意事项
1.结论示意图
- 对于
intersect
、minus
,oracle支持,mysql不支持,可以变通(in
或exists
)实现
2.创建表和数据
drop table if exists student;
create table student (
id int
);
insert into student (id) values (1);
insert into student (id) values (2);
insert into student (id) values (3);
insert into student (id) values (4);
select * from student;
3.查询
3.1. A集合
select * from student where id in (1,2,3);
3.2. B集合
select * from student where id in (2,3,4);
3.3. 交集intersect
(A ∩ B)
- oracle支持,mysql不支持(可以变通实现)
select * from student where id in (1,2,3)
intersect
select * from student where id in (2,3,4);
select * from student where id in (1,2,3)
and id in (
select id from student where id in (2,3,4));
3.4. 差集minus
- oracle支持,mysql不支持(可以变通实现)
3.4.1.左差集minus
(A - B)
select * from student where id in (1,2,3)
minus
select * from student where id in (2,3,4);
select * from student where id in (1,2,3)
and id not in (
select id from student where id in (2,3,4));
3.4.2 右差集minus
(B - A)
select * from student where id in (2,3,4)
minus
select * from student where id in (1,2,3);
select * from student where id in (2,3,4)
and id not in (
select id from student where id in (1,2,3));
3.5. 并集union
(A ∪ B)
select * from student where id in (1,2,3)
union
select * from student where id in (2,3,4);
3.6. 和集 union all
(A + B)
select * from student where id in (1,2,3)
union all
select * from student where id in (2,3,4);
3.7. 补集(A minus B) union (B minus A)
[(A - B) ∪ (B - A)]或 (A union B) minus (A intersect B)
[(A ∪ B) - (A ∩ B)] 。A ∩ B在A ∪ B的补集。
- oracle支持,mysql不支持(可以变通实现)
(
select * from student where id in (1,2,3)
minus
select * from student where id in (2,3,4)
)
union
(
select * from student where id in (2,3,4)
minus
select * from student where id in (1,2,3)
);
(
select * from student where id in (1,2,3)
and id not in (
select id from student where id in (2,3,4))
)
union
(
select * from student where id in (2,3,4)
and id not in (
select id from student where id in (1,2,3))
);
(
select * from student where id in (2,3,4)
union
select * from student where id in (1,2,3)
)
minus
(
select * from student where id in (2,3,4)
intersect
select * from student where id in (1,2,3)
);
select * from
(
select * from student where id in (1,2,3)
union
select * from student where id in (2,3,4)
)
where id not in
(
select id from student where id in (1,2,3)
and id in (
select id from student where id in (2,3,4))
);