当前位置: 代码迷 >> Sql Server >> 交集有关问题
  详细解决方案

交集有关问题

热度:45   发布时间:2016-04-27 19:00:19.0
交集问题
有a,b两个表,两表均有同类型字段f。

问:能不能用一个sql语句实现下面的目标:
1.   如果a,b没有数据,则返回空集
2.   如果a,b均有数据,则返回两者的交集
3.   如果a,b任一个表没有数据,则返回有数据的那个表的集合

谢谢!

------解决方案--------------------
try:
select * from a where not exists(select 1 from b)
union
select * from b where not exists(select 1 from a)
union
select * from a where exists(select 1 from b where f=a.f)
  相关解决方案