The name column contains items like 'John Smith', 'Elsa John Sanders', 'LilJohn Thomson', 'John'. How can I structure a query just to return the names with John but not LilJohn.
I cannot do a LIKE '%John%' as it would return 'LilJohn Thomson'.
The name column contains items like 'John Smith', 'Elsa John Sanders', 'LilJohn Thomson', 'John'. How can I structure a query just to return the names with John but not LilJohn.
I cannot do a LIKE '%John%' as it would return 'LilJohn Thomson'.
Looks like this is the same as: Search for "whole word match" in MySQL
The approach uses a slick regular expression.
Assuming a word is defined by a space, you can do:
where concat(' ', col, ' ') like '% John %'
The following expression should find all "John"s
a = 'John' OR
a LIKE 'John %' OR
a LIKE '% John %' OR
a LIKE '% John'
Please note that some other tricks also work, but may severely hit performance, like
CONCAT(" ",a," ") LIKE "% John %"
You can use REGEXP function.
Try this:
SELECT *
FROM tableA a
WHERE a.name REGEXP '[[:<:]]John[[:>:]]'