简体   繁体   中英

Select / Update records in one table that are LIKE words selected from another table column with multiple records

I need to find records in product.title that are like one of many words. Currently this is done by a long query like this one:

Select product.product_id, product.name from product WHERE product.name like '%word1%' OR product.name like '%word2%' OR product.name like '%word3%', etc (too many words).

Decided to put these words in a new table for easier management and build a query with the records from that table

TABLE banned_words.word
word1
word2
word3
etc

I tried GROUP_CONCAT with a separator before and after banned_words.word so that I can use it in a sub-query, but the result was larger than the 1024 characters limit. I don't suppose I can use a join with a sub-string

Any suggestions?

You could use the exists operator:

SELECT product.product_id, product.name
FROM   product
WHERE  EXISTS (SELECT *
               FROM   banned_words
               WHERE  product.name LIKE CONCAT('%', word, '%')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM