使用PL/SQL Developer比较两个SELECT语句

5
假设我有两个 SELECT 查询针对同一张表,我需要逐列比较它们。
查询 1:
select * from mytable t1 where  t1.code = '100'

返回:

id           name
1            A
2            B

查询2:

select * from mytable t2 where  t2.code = '999'

返回:

id           name
1            A
2            C

在我的情况中,这两个查询返回的行数相同。

期望的结果

id           name_t1     name_t2
2            B           C

我该如何使用 PL/SQL developer 找到它们之间的数据差异(最好使用工具避免查询)?

我的 PL/SQL Developer 版本为8.0.3.1510


我认为,唯一的方法可能是提供一个导出到Excel选项MINUS也可能对您有帮助。 - Maheswaran Ravisankar
3个回答

8
你可以使用MINUS
select * from mytable t1 where  t1.code = '100'

MINUS 
select * from mytable t2 where  t2.code = '999';

MINUS运算符用于从结果集中删除只在第二个查询中找到的行,仅保留在第一个查询中找到且不在第二个查询中找到的行。


对于我的情况,两个查询返回相同数量的行。需要级联比较它们。 - user5896972
嗨,你想比较行数吗?还是数据的差异? - Ramki
逐列比较数据 - user5896972
嗨,然后减法应该很好,尝试使用您的表并发布结果。 - Ramki

4

有很多方法可以检查两个查询之间的差异;如果您需要数据集之间的差异,可以尝试使用 MINUS

SQL> select * from mytable t1 where  t1.code = '100'
  2  minus
  3  select * from mytable t2 where  t2.code = '999';

  ID CODE NAME
---- ---- ----------
   1  100 A
   2  100 B

SQL> select * from mytable t2 where  t2.code = '999'
  2  minus
  3  select * from mytable t1 where  t1.code = '100';

  ID CODE NAME
---- ---- ----------
   1  999 A
   2  999 C

通过使用两个MINUS,你可以得到T1-T2 AND T2-T1

SQL> select 'T1 MINUS T2' TYPE, t1_t2.* from
  2  (
  3      select * from mytable t1 where  t1.code = '100'
  4      minus
  5      select * from mytable t2 where  t2.code = '999'
  6  ) t1_t2
  7  union all
  8  select 'T2 MINUS T1' TYPE, t2_t1.* from
  9  (
 10      select * from mytable t2 where  t2.code = '999'
 11      minus
 12      select * from mytable t1 where  t1.code = '100'
 13  ) t2_t1;

TYPE          ID CODE NAME
----------- ---- ---- ----------
T1 MINUS T2    1  100 A
T1 MINUS T2    2  100 B
T2 MINUS T1    1  999 A
T2 MINUS T1    2  999 C

如果你需要基于“键”字段检查字段差异,你需要使用 JOIN

SQL> select  id, t1.name as name_t1, t2.name as name_t2
  2  from myTable t1
  3    inner join myTable t2 using(id)
  4  where t1.name != t2.name
  5    and t1.code = '100'
  6    and t2.code = '999';

  ID NAME_T1    NAME_T2
---- ---------- ----------
   2 B          C

0

从 myTable t1 右连接 myTable t2 右连接 t1.name=t2.name 选择 id, t1.name as name_t1, t2.name as name_t2 其中 t1.name 为空


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接