SQL如何比较两个集

来源:百度知道 编辑:UC知道 时间:2024/06/05 10:00:23
比如有表a,
ID value
1,A
1,B
1,C 这样的三行,

有表b
ID value
2,B
2,A
2,C 这样的三行
select value from a where ID=1
select value from b where ID=2
这样的两个返回结果如何去比较呢,不考虑A,B,C的前后顺序?谢谢了
如果b表是这样的
ID value
2,B
2,A
或者
2,B
2,A
2,C
2,D
这时和a进行比较的时候,怎样判断他们是不等的,或者说现在给出b的返回集是B,A,C那怎样得出a中也是这三个返回集的行,而且是只有这三行的,多或少都不对

取得结果集的相同部份
select * from
(select value from a where ID=1)x
inner join
(select value from b where ID=2)y
on x.value=y.value

取得结果集的不同部份
select * from
(select value from a where ID=1)x
full join
(select value from b where ID=2)y
on x.value=y.value
where (x.value is null) or (y.value is null)

不明白你到底要什么结果
if exists(
select * from
(select value from a where ID=1)x
full join
(select value from b where ID=2)y
on x.value=y.value
where (x.value is null) or (y.value is null)
)
print '两个结果集不同'
else
print '结果集相同'