簡體   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