数据库复习总结(9)-联合查询

联合查询

-》将多个查询的结果集合并成一个结果集

-》联合要求:

结果集列数要一致

对应列的类型要一致

-》关键字:

union--合并,排序,消除重复项

union all--合并,不排序,不消除重复项

except--差集

intersect--交集

-》用处:在查询结果处显示汇总

例子:

select cid from ClassInfo
select sid from StudentInfo

1.union 


select
cid from ClassInfo union--合并,排序,消除重复项 select sid from StudentInfo

2.union all

select cid from ClassInfo
union all--合并,不排序,不消除重复项
select sid from StudentInfo

3.except

select cid from ClassInfo
except--差集
select sid from StudentInfo

select sid from StudentInfo
except--差集
select cid from ClassInfo

4.intersect

select sid from StudentInfo
intersect--交集
select cid from ClassInfo

原文地址:https://www.cnblogs.com/mhq-martin/p/8024757.html