Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL select string that doesn't contain Y but contains X

I'm attempting to select all of the instances where this database field has a string but does not contain a specific value.

Example: I'm trying to select every instance of "red chair" where "red chair" is not proceeded by "big". If I were to run that query in the following table, I'd only get the row with the ID of 2 back:

+----------+--------------+---------+
| ID       |        content         | 
+----------+------------------------+
| 1        | The big red chair      |
| 2        | I have a red chair     |
| 3        | I have a big chair     |
+----------+------------------------+

Thanks in advance!

like image 731
Katzumi Avatar asked Oct 05 '11 16:10

Katzumi


1 Answers

You can use:

LIKE '%red chair%' AND NOT LIKE '%big%'

edit: Fixed LIKE matching strings

like image 71
kand Avatar answered Sep 22 '22 09:09

kand