【问题标题】:Compare two SELECT statement using PL/SQL Developer使用 PL/SQL Developer 比较两个 SELECT 语句
【发布时间】:2016-03-29 06:05:17
【问题描述】:

假设我在同一张表上有两个SELECTquery,我需要逐列比较它们。

查询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 开发人员 版本 8.0.3.1510

【问题讨论】:

标签: sql oracle plsqldeveloper


【解决方案1】:

你可以使用MINUS

select * from mytable t1 where  t1.code = '100'

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

MINUS 通过从结果中删除仅在第二个查询中找到的所有行,为您提供在第一个查询中找到的行,而不是在第二个查询中找到的行

【讨论】:

  • 就我而言,两个查询都返回相同的行数。并且需要比较它们级联
  • 嗨,你想比较行数吗?数据的差异
  • 逐列比较数据
  • 嗨,那么 MINUS 应该不错,试试你的表并发布结果。
【解决方案2】:

有很多方法可以检查两个查询之间的差异;如果您需要数据集之间的差异,您可以尝试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

如果你需要检查字段差异,基于一个'key'字段,你需要一个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

【讨论】:

    【解决方案3】:

    选择 id,t1.name 作为 name_t1,t2.name 作为 name_t2 从我的表 t1 右加入 myTable t2 右连接 t1.name=t2.name 其中 t1.name 为空

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-29
      • 2022-11-30
      相关资源
      最近更新 更多