简体   繁体   中英

MySQL Select rows where column contains only values from a set

Is there a way to select rows where one of the column contains only predefined values? I've been using this, but it returns any rows where my column contains at least one of the values (which is exactly what it's suppose to do, I know). But I'm looking for a way to only select rows that have ONLY my keywords in the keyword column.

SELECT * 
FROM 
    `products`.`product` 
WHERE 
    keywords LIKE '%chocolate%' 
AND keyword LIKE '%vanilla%';

I'm looking for something to do this

SELECT * 
FROM 
   `products`.`product` 
WHERE 
   keywords **ONLY** LIKE '%chocolate%' 
OR keyword LIKE '%vanilla%';

I've tried to make the most sense with this question, hopefully someone has a magic solution for me!

UPDATE/Examples: Keywords: chocolate, sugar, milk, oats

Using the above keywords, I would want the first two results returned, but not the last two:

Product1: chocolate, sugar Product2: chocolate Product3: chocolate, sugar, milk, oats, bran Product4: chocolate, sugar, salt

My column contains a comma separated list of all keywords applicable to that product row.

SELECT * FROM `products`.`product` WHERE keywords = 'chocolate' OR keyword = 'vanilla';

根据您提供的信息,可以完成您想要的工作-还是有更多呢?

What about this:

SELECT *
 FROM `products`.`product`
 WHERE (keywords LIKE '%chocolate%' AND keyword NOT LIKE '%vanilla%')
 OR (keywords NOT LIKE '%chocolate%' AND keyword LIKE '%vanilla%');

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