繁体   English   中英

如何比较sql中具有不同列数的两个表

[英]how to compare two tables having different number of columns in sql

有 2 个表 - table1 带有列source ( varchar ) 和 table2 带有列new_source ( varchar ) 和id ( number )。

我想将 table1 与 table2 进行比较。 输出应具有new_sourceid ,它们不存在于 table2 中,但存在于 table1 中。

下面是一个例子:

表格1

在此处输入图片说明

表2

在此处输入图片说明

输出:

在此处输入图片说明

我试过使用不存在和减号,但我在输出中只得到new_source而不是id

请帮助我编写查询。

select *
from table2
where new_source in
  (select source from table1
   minus
   select new_source from table2);

嗯。 . . 不清楚id来自哪里。 所以,我在想:

select listagg(source, ',') within group (order by source), 10 as id
from table1 t1
where not exists (select 1
                  from table2 t2
                  where t2.source = t1.source and t2.source = 10
                 );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM