2009-12-29 69 views
0

我有两个表A & B我想有一个查询: 返回TRUE只有当两个表是相同的(我的意思是A中的每一行都存在B &反之,无论是线序)PostgreSQL查询两个表之间的相等性

我使用关键字EXCEPT,但在许多情况下不工作

感谢您的帮助。

+0

请发表你试了一下,以及表A和表B中的列表。 – 2009-12-29 10:15:30

+0

您的数据集有多大?除了遇到什么问题? – 2009-12-29 10:16:57

+0

数据集非常小(少于20行)并且查询仅用于调试而不用于生产。 – adam 2009-12-29 10:34:06

回答

6
select * from tablea except all select * from tableb 

返回tablea中不存在的所有行。

做它周围的其他方法

select * from tableb except all select * from tablea 

收益表B中的所有行不TableA中存在。

所以,现在我们可以:

select count(*) from (select * from tablea except all select * from tableb) x; 

得到的TableA中 “坏” 的行数和:

select count(*) from (select * from tableb except all select * from tablea) x; 

得到的表B中的 “坏” 的行数。

表是相同的,如果这两点都为0,并因为无论计数可大于零少,那么我们可以测试数的总和为0:

select 0 = (select count(*) from (select * from tablea except all select * from tableb) x) + (select count(*) from (select * from tableb except all select * from tablea) x); 
+0

谢谢你!除了+运营商的位置,它工作正常。 – adam 2009-12-29 13:34:56

+1

如果它解决了你的问题,你应该接受答案 – 2011-01-30 23:11:27