Mysql-5.5

過濾 SELECT 查詢的結果以避免列值中的某些單詞?

  • October 16, 2014

我有一個名為videos的表格,其中有一列名為title,並且有一組單詞(目前大約 50 個單詞,但單詞數可能會增加),這是用於過濾內容的知識庫。

例如

色情, 色情, 成人, 胸部, 贓物, 乳房

偽查詢可能像

SELECT * FROM videos WHERE title NOT LIKE (porn, pornography, Adult, Boobs, Booty, Tits)

我檢查了NOT LIKE,但我必須這樣做

SELECT * FROM videos WHERE title NOT LIKE ‘%porn%’ AND title NOT LIKE ‘%pornography%’ AND title NOT LIKE ‘%Adult%’ AND title NOT LIKE ‘%Booty%’ AND title NOT LIKE ‘%Boobs%’ AND title NOT LIKE ‘%Tits%’

這是非常不方便和低效的。那麼還有其他更好的方法嗎?

我已經在 SQL Server 中編寫了此程式碼,因此您可能必須轉換為 MySQL,但這應該適合您。

CREATE TABLE #ExcludeList (ExcludeVal varchar(50))

CREATE TABLE #VideoList (VideoName varchar(500))

INSERT INTO #ExcludeList VALUES ('Prob1'),('Prob2'),('DontUse1')

INSERT INTO #VideoList VALUES ('This is a prob1'),
           ('This is clean'),('This is also clean'),
           ('DontUse1 this one is bad')

SELECT *
FROM #VideoList VL
WHERE NOT EXISTS (SELECT 1 FROM #ExcludeList EL
                   WHERE VL.VideoName LIKE '%' + EL.ExcludeVal + '%')

引用自:https://dba.stackexchange.com/questions/80370