Sql 如何从Oracle中的给定列表中获取不存在的记录列表?
如何从给定的输入条件中获取不可用(不存在)记录列表列表 如果我们使用Not IN运算符,它将导致表中所有不匹配的记录。但是我想从给定的输入条件中得到不匹配的记录。我已经给出了一些示例 表格名称:国家/地区Sql 如何从Oracle中的给定列表中获取不存在的记录列表?,sql,oracle,Sql,Oracle,如何从给定的输入条件中获取不可用(不存在)记录列表列表 如果我们使用Not IN运算符,它将导致表中所有不匹配的记录。但是我想从给定的输入条件中得到不匹配的记录。我已经给出了一些示例 表格名称:国家/地区 可以使用一组选择并集和减号 ( select 'AU' from dual union select 'IN' from dual union select 'ZA' from dual union select 'DK' from dual union
可以使用一组选择并集和减号
( 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')
从逻辑上讲,您希望从输入列表中返回
国家/地区
表中不存在的国家/地区代码。这可以非常简单地用notexists
来表示
唯一的困难是您需要将国家/地区代码的输入列表转换为行列表。您可以通过使用一系列的union-all
s来实现这一点。或者,由于您使用的是Oracle,因此可以使用SYS.DBMS\u DEBUG\u 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)
这并没有给我预期的输出。你能检查一下吗?