Okay...
Rows in database...
1: fred
2: fred john peter paul
3: john
4: peter smith
5: paul
6: john jones, paul smith, peter smith
7: peter smithson
Queries:
SELECT * FROM table WHERE name LIKE 'fred'
- Will match row 1
SELECT * FROM table WHERE name LIKE '%fred%'
- Will match row 1 and row 2
SELECT * FROM table WHERE name LIKE 'peter'
- Will match no rows
SELECT * FROM table WHERE name LIKE '%peter%'
- Will match rows 2, 4, 6 and 7
SELECT * FROM table WHERE name LIKE 'peter smith'
- Will match row 4
SELECT * FROM table WHERE name LIKE '%peter smith%'
- Will match row 4, 6, and 7
The problem you're having is your trying to do an exact match like queries 1, 3, and 5 which will only work if the column contains that value and that value alone.
You need the % either side of the value to allow for there being other data in the row.
The only way to do it is to ensure that there is some form of seperator between the values in the field, or to use regular expressions
You could for example ensure that there is a , before and after every word in the field:-
Rows in database...
1: ,fred,
2: ,fred,john,peter,paul,
3: ,john,
4: ,peter smith,
5: ,paul,
6: ,john jones,paul smith,peter smith,
7: ,peter smithson,
Then use something like
SELECT * FROM table WHERE name LIKE '%,peter smith,%'
- Which would bring back row 6 and 4 only
The better solution would be to use regular expressions to ensure that the it matches a whole word.
See here for details http://www.mysql.com/doc/en/Regexp.html
In fact this....
SELECT * FROM table WHERE name LIKE '[[:<:]]peter smith[[:>:]]'
Might well do the job!