this is what I have:
SELECT `id`, `names` FROM `screenplays` WHERE `names` LIKE '%joe%mike%'
the field names contains:
row1: joe
row2: mike
row3: joe,mike
how do I return all 3 rows as a result ?
Use
SELECT `id`, `names`
FROM `screenplays`
WHERE `names` LIKE '%joe%'
OR `names` LIKE '%mike%'
If the names are separated by a comma, and you don't want names like joey when you specify joe, you can expand on the query like:
SELECT `id`, `names`
FROM `screenplays`
WHERE `names` LIKE 'joe'
OR `names` LIKE '%,joe'
OR `names` LIKE 'joe,%'
OR `names` LIKE '%,joe,%'
And similarly for each keyword
All good answers, but I prefer to use MySQL's excellent support for regular expressions:
SELECT `id`, `names` FROM `screenplays` WHERE `names` REGEX 'joe|mike'
To take in to account comma separated names, and also white space use a more advanced regular expression:
SELECT `id`, `names` FROM `screenplays` WHERE `names` REGEX '(^|,) *(joe|mike) *($|,)'
Which breaks down to:
(^|,) # begin at start of line, or after a comma * # ignore any spaces (joe|mike) # names to match * # ignore any spaces ($|,) # ends at end of line or a comma
SELECT `id`, `names` FROM `screenplays`
WHERE concat(',', `names`, ',') LIKE '%,mike,%' or concat(',', `names`, ',') LIKE '%,joe,%'
Adding comma to names
prevent returning carlton
when you are searching carl
.
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.