读书人

交集有关问题

发布时间: 2012-02-05 12:07:14 作者: rapoo

交集问题
有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)

读书人网 >SQL Server

热点推荐