tags:

views:

36

answers:

2

I am trying to search a table where the field name does not have a list of strings in it. I use:

SELECT * 
 FROM members  
WHERE name NOT IN ('bob', 'jim', 'leroy');

but it still returns matches containing those words. I have searched high and low for the answer to this. Can anyone help?

+3  A: 

name NOT IN ('bob', 'jim', 'leroy') is equivalent to name!='bob' and name!='jim' and name!='leroy'.

Perhaps you want

name not like '%bob%' and name not like '%jim%' and name not like '%leroy%'

instead?

bemace
yeah but I thought there would be a more efficient way, perhaps not
Adam Coburn
@Adam within mysql I think that's as good as it gets. If you need to improve performance look into an external search engine like http://sphinxsearch.com .
bemace
MySQL has [native Full Text Search functionality](http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html)
OMG Ponies
@OMG Ponies - I suppose I should have mentioned that, but I didn't because it's *way* slower. Might be good enough for his needs though.
bemace
Slower than a table scan? I don't think so, but constraining because of only supporting MyISAM.
OMG Ponies
@OMG Ponies - No, I meant slower than Sphinx. To me it seems like the window where it makes sense to use full-text search but not a far-superior option like Sphinx or lucene is pretty small.
bemace
+1  A: 

This only NOT matches values that are exactly one of the names. You can try WHERE name NOT LIKE "%bob%" AND NOT LIKE "%jim%" AND NOT LIKE "%leroy%"

slosd