1

我想知道两个记录集是否不同。我为此使用了一个full outer join。简单的例子:

select count(*) from 
(
 select 1 as c
 union 
 select 2 as c 
) t1
full outer join 
(
 select 1 as c 
 union 
 select 3 as c
) t2 on t1.c = t2.c
where t1.c is null or t2.c is null

SQLFiddle

如果记录集相等,则count()返回0

有没有更好的方法来实现这一目标?

4

2 回答 2

1

如果c您的记录集中是唯一的,您可以使用GROUP BYand来完成HAVING,如下所示:

SELECT COUNT(*) FROM (
    SELECT c FROM (
        SELECT c FROM record_set_1
    UNION ALL
        SELECT c FROM record_set_2
    ) all_data
    GROUP BY c
    HAVING COUNT(*) <> 2
) non_matched

SQLFiddle

于 2013-11-13T16:38:34.700 回答
0

你可以使用EXISTS

IF EXISTS(SELECT null FROM t1 where c NOT IN (SELECT c FROM t2))
OR EXISTS(SELECT null FROM t2 where c NOT IN (SELECT c FROM t1))

当然,这并不能告诉您哪些记录不匹配 - 如果您想知道您的原始查询就可以了。

于 2013-11-13T16:41:27.967 回答