![](/img/trans.png)
[英]Oracle SQL - Using joins to find values in one table, and not another
[英]Oracle SQL — find the values NOT in a table
拿这个表词
WORD
Hello
Aardvark
Potato
Dog
Cat
这个清单:
('Hello', 'Goodbye', 'Greetings', 'Dog')
如何返回不在单词表中但在我的列表中的单词列表?
如果我有一个“包含所有可能的单词”的表,我可以这样做:
SELECT * from ALL_WORDS_TABLE
where word in ('Hello', 'Goodbye', 'Greetings', 'Dog')
and word not in
(SELECT word from WORDS
where word in ('Hello', 'Goodbye', 'Greetings', 'Dog')
);
但是我没有这样的表。 怎么办呢?
此外,构建新表不是一种选择,因为我没有这种访问级别。
不是将列表值硬编码为行,而是使用DBMS_DEBUG_VC2COLL
将分隔列表动态转换为行,然后使用MINUS
运算符消除第二个查询中不在第一个查询中的行:
select column_value
from table(sys.dbms_debug_vc2coll('Hello', 'Goodbye', 'Greetings', 'Dog'))
minus
select word
from words;
您可以将列表转换为如下视图:
select 'Hello' as word from dual
union all
select 'Goodbye' from dual
union all
select 'Greetings' from dual
union all
select 'Dog' from dual
然后你可以从中选择:
select * from
(
select 'Hello' as word from dual
union all
select 'Goodbye' from dual
union all
select 'Greetings' from dual
union all
select 'Dog' from dual
)
where word not in (select word from words);
可能不像你希望的那样整洁的解决方案......
你说你没有足够的权限来创建表,所以你可能也不能创建类型 - 但是如果你能在数据库中找到一个合适的类型“你可以”,你可以这样做:
select * from table (table_of_varchar2_type('Hello','Goodbye','Greetings','Dog'))
where column_value not in (select word from words);
这里table_of_varchar2_type
被想象为一个类型的名称,定义如下:
create type table_of_varchar2_type as table of varchar2(100);
您可能能够找到的一种类型是SYS.KU$_VCNT
,它是一个TABLE OF VARCHAR2(4000)。
尝试此解决方案:
SELECT
a.word
FROM
(
SELECT 'Hello' word FROM DUAL UNION
SELECT 'Goodbye' word FROM DUAL UNION
SELECT 'Greetings' word FROM DUAL UNION
SELECT 'Dog' word FROM DUAL
) a
LEFT JOIN ALL_WORDS_TABLE t ON t.word = a.word
WHERE
t.word IS NULL
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.