如何在SQL中显示除标题外的标题中的字符串

时间:2017-03-23 06:41:08

标签: sql regex database oracle

我有以下情景

"原"在标题栏中找到的单词除了以下异常

示例列(标题)值,如 -

  • 原创电影

  • 奇妙的恩典(原百老汇演员              记录)

例外清单:

 Original%Cast ,Original%Broadway, Original%Motion, Original%Score, Original%Sound, Original%Game ,Original%TV, Original%Television, Original%Off, Original%Series,

我已经编写了像这样的示例查询

  select  count(1)
    from table_name
    where product_id = :PRODUCT_ID
    and instr(upper(title),'ORIGINAL') > 0
    and a
(
title not like '%Original%Cast%'
--or title not like 'Original%Broadway'
--or title not like ....

);

请使用正则表达式建议查询,如子句

中的列表in('%Original%Cast%','Original%Broadway','....)所示

2 个答案:

答案 0 :(得分:0)

你应该选择:

select  count(1)
from table_name
where product_id = :PRODUCT_ID
and instr(upper(title),'ORIGINAL') > 0
and 
(
title not like '%Original%Cast%'
or title not like 'Original%Broadway'
or title not like ....
....
)

请注意,not like语句位于单独的括号组中。

答案 1 :(得分:0)

这是一种方式:

WITH table_name AS (SELECT 'The Original Case' title FROM dual UNION ALL
                    SELECT 'The First Case' title FROM dual UNION ALL
                    SELECT 'The Super Case' title FROM dual UNION ALL
                    SELECT 'The Super Case - Original Cast Recording' title FROM dual UNION ALL
                    SELECT 'The Super Case - Original Broadway Show' title FROM dual UNION ALL
                    SELECT 'The Super Case - Original Motion Picture' title FROM dual UNION ALL
                    SELECT 'The Super Case - Original Score' title FROM dual UNION ALL
                    SELECT 'The Super Case - Original Soundtrack' title FROM dual UNION ALL
                    SELECT 'The Super Case - Original Video Game' title FROM dual UNION ALL
                    SELECT 'The Super Case - Original TV show' title FROM dual UNION ALL
                    SELECT 'The Super Case - Original television show' title FROM dual UNION ALL
                    SELECT 'The Super Case - Original off-site production' title FROM dual UNION ALL
                    SELECT 'The Super Case - Original theatre series' title FROM dual UNION ALL
                    SELECT 'The TV Original Case' title FROM dual)
SELECT *
FROM   table_name
WHERE  NOT regexp_like(title, 'original.*(cast|broadway|motion|score|sound|game|tv|television|off|series)', 'i')
AND    regexp_like(title, 'original', 'i');

TITLE
---------------------------------------------
The Original Case
The TV Original Case

第一个regexp_like查找“原始”模式,后跟任何字符,然后是cast,broadway,....或者系列。修饰符参数中的i表示搜索不区分大小写。然后我们在它前面添加NOT来折扣具有该模式的行。