以下SQL概念性地复制了我试图解决的问题。尽管传递了NOT IN子句,但所有三条记录都被返回。
SELECT * FROM (
SELECT 'JACK' AS VALUE FROM DUAL
UNION
SELECT 'JOHN' AS VALUE FROM DUAL
UNION
SELECT 'BOB' AS VALUE FROM DUAL
) WHERE VALUE NOT IN (SELECT 'BOB,JOHN' FROM DUAL);
我有一个包含分隔字符串的表,我想将其用作从数据集中排除记录的条件。但是,我遇到的问题是返回的字符串不会分解为其分隔的项目。我想将上述内容翻译成:
SELECT * FROM (
SELECT 'JACK' AS VALUE FROM DUAL
UNION
SELECT 'JOHN' AS VALUE FROM DUAL
UNION
SELECT 'BOB' AS VALUE FROM DUAL
) WHERE VALUE NOT IN ('BOB','JOHN');
答案 0 :(得分:1)
'BOB,JOHN'
不是两个字符串值的列表,它是一个字符串值,恰好在字符串中包含逗号,并且:
'JACK' = 'BOB,JOHN'
'JOHN' = 'BOB,JOHN'
'BOB' = 'BOB,JOHN'
全部为false,因此您的查询将返回NOT IN
过滤器匹配的所有行。
您可以使用分隔符字符包围值并列出,并测试该值是否不是列表的子字符串,如下所示:
SELECT *
FROM (
SELECT 'JACK' AS VALUE FROM DUAL UNION ALL
SELECT 'JOHN' AS VALUE FROM DUAL UNION ALL
SELECT 'BOB' AS VALUE FROM DUAL
)
WHERE INSTR( ',' || 'BOB,JOHN' || ',', ',' || value || ',' ) = 0
或者您可以使用用户定义的集合:
CREATE OR REPLACE TYPE stringlist IS TABLE OF VARCHAR2(20);
然后使用MEMBER OF
运算符来测试值是否是集合的成员:
SELECT *
FROM (
SELECT 'JACK' AS VALUE FROM DUAL UNION ALL
SELECT 'JOHN' AS VALUE FROM DUAL UNION ALL
SELECT 'BOB' AS VALUE FROM DUAL
)
WHERE VALUE NOT MEMBER OF StringList( 'BOB', 'JOHN' );
答案 1 :(得分:0)
您可以使用regexp_substr解决该问题:
SELECT * FROM (
SELECT 'JACK' AS VALUE FROM DUAL
UNION
SELECT 'JOHN' AS VALUE FROM DUAL
UNION
SELECT 'BOB' AS VALUE FROM DUAL
)
WHERE VALUE NOT IN (SELECT regexp_substr('BOB,JOHN','[^,]+', 1, LEVEL) FROM dual CONNECT BY regexp_substr('BOB,JOHN', '[^,]+', 1, LEVEL) IS NOT NULL)