0

例如

简化案例表:

表 A 包含表 B 中缺少的行条目

select count(1) from table_a; --> returns 5 results
select count(1) from table_b; --> returns 4 results
select count(1) from table_a, table_b2 b 
where b.id_ab like a.id_ab; --> returns 4 results
select count(1) from table_a, table_b2 b 
where b.id_ab not like a.id_ab; --> returns unexpected result

SQL:

试过这个(除了)但遇到错误。

select a.id_ab from table_a a, table_b b except select a.id_ab from table_a, table_b2 b 
where b.id_ab not like a.id_ab; 

或者如何使用联合来做到这一点?例如

(Select * from table_a except select * from table_b) Union All (Select * from table_b     except select record_id from table_a);

预期结果:

结果

谢谢你。

4

2 回答 2

2
select a.id_ab 
from table_a a 
    LEFT JOIN table_b b on a.id_ab = b.id_ab 
where b.id_ab is null
于 2013-01-08T07:59:35.443 回答
1

使用下面的查询(添加左外连接)。

它将获取 table_a 中不匹配的数据。

 select count(1) from table_a, table_b2 b 
 where b.id_ab(+) = a.id_ab;

如果您只想缺少行,请使用以下查询

 select count(1) from table_a, table_b2 b 
 where b.id_ab <> a.id_ab;

如果在查询之间使用 UNION 列应该是相等的。

例子

   SELECT col1,col2 from table_a
   UNION
   SELECT col1,col2 from table_b

但不喜欢

  SELECT col1,col2 from table_a
   UNION
   SELECT col1 from table_b
于 2013-01-08T08:07:21.990 回答