我有两个表A和B,我希望有一个查询:如果两个表相同,则返回TRUE(我的意思是A中的每一行都存在于B中,反之亦然,无论行顺序如何)
我使用了关键字EXCEPT但它在许多情况下都不起作用
谢谢你的帮助.
小智 6
select * from tablea except all select * from tableb
Run Code Online (Sandbox Code Playgroud)
返回tablea中不存在的tablea中的所有行.
反过来做这件事
select * from tableb except all select * from tablea
Run Code Online (Sandbox Code Playgroud)
返回tableb中tablea中不存在的所有行.
所以,现在我们可以:
select count(*) from ( select * from tablea except all select * from tableb ) x;
Run Code Online (Sandbox Code Playgroud)
获取tablea中"坏"行的数量,并且:
select count(*) from ( select * from tableb except all select * from tablea ) x;
Run Code Online (Sandbox Code Playgroud)
计算tableb中的"坏"行数.
如果两个计数都为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 );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1047 次 |
| 最近记录: |