2016-09-21 89 views
0

如何從給定輸入條件獲取不可用(不存在)記錄列表的列表?如何從Oracle中的給定列表中獲取不存在記錄列表

如果我們使用Not IN操作符,它將導致表中所有不匹配的記錄,但是我想從給定的輸入條件中得到不匹配的記錄。我舉了一些樣例,請你幫我解答一下。

表名:國家

enter image description here

回答

1

從邏輯上講,你想從你的輸入列表返回國家代碼在country表中不存在。這可以很簡單地用not exists來表示。

唯一的困難是您需要將國家代碼的輸入列表轉換爲行列表。你可以通過使用一系列union all來做到這一點。或者,由於使用的是Oracle,你可以使用SYS.DBMS_DEBUG_VC2COLL執行列表到餐桌轉型,我認爲使查詢更具可讀性:

select i.column_value as country_code 
    from table(SYS.DBMS_DEBUG_VC2COLL('AU', 'IN', 'ZA', 'DK', 'CH', 'NL')) i 
where not exists (select null 
        from country c 
        where c.country_code = i.column_value) 
+0

謝謝!它爲我工作... – Subburaj

0

可以使用一組選擇工會和負

(select 'AU' from dual 
union 
select 'IN' from dual 
union 
select 'ZA' from dual  
union 
select 'DK' from dual 
union 
select 'CH' from dual  
union 
select 'NL' from dual ) 
minus 
SELECT countrycode 
FROM country 
WHERE countrycode IN ('AU','IN','ZA','DK','CH','NL') 
+0

這不是給我的預期輸出。你能檢查一下嗎? – Subburaj

+0

我已更新答案..用於和不....不在.. – scaisEdge

相關問題