2017-03-23 42 views
-1
在標題中除例外列表中的字符串

我有以下情形如何顯示在SQL

「原始」一詞在標題欄中發現,除了下面的例外

樣品列(標題)值一樣 -

  • 原始運動畫面

  • 奇異恩典(原百老匯演員 錄音)

例外列表:

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 .... 

); 

請建議用正則表達式查詢,如第

回答

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查找「原生態」後跟任意字符,再其次是無論是演員,百老匯,...,或一系列的模式。修飾符參數中的i表示搜索不區分大小寫。然後我們在它前面添加NOT來折扣具有該模式的行。

+0

感謝@Boneist的查詢,它工作正常。 – Madhava

0

您應該顯示列表in('%Original%Cast%','Original%Broadway','....)跟隨:

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語句位於單獨的括號組中。

+0

感謝您的查詢。但我需要使用正則表達式的查詢REGEXP_INSTR() – Madhava

+0

對不起,在你原來的問題中,你有「請在sql中使用或不使用regexp提出查詢。」在最後... –