简体   繁体   English

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

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

There are 2 tables - table1 with columns source ( varchar ) and table2 with columns new_source ( varchar ) and id ( number ).有 2 个表 - table1 带有列source ( varchar ) 和 table2 带有列new_source ( varchar ) 和id ( number )。

I want to compare table1 with table2.我想将 table1 与 table2 进行比较。 Output should have the new_source and id which are not present in table2 but present in table1.输出应具有new_sourceid ,它们不存在于 table2 中,但存在于 table1 中。

Here's an example:下面是一个例子:

Table1表格1

在此处输入图片说明

Table2表2

在此处输入图片说明

Output:输出:

在此处输入图片说明

I have tried using not exists and minus but I get only new_source but not id in the output.我试过使用不存在和减号,但我在输出中只得到new_source而不是id

Kindly help me in writing the query.请帮助我编写查询。

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

Hmmm .嗯。 . . . . it is not clear where the id comes from.不清楚id来自哪里。 So, I'm thinking:所以,我在想:

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