2016-09-21 91 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

相关问题