views:

124

answers:

3

I would like to run something like:

select * from table where field in ("%apple%", "%orange%")

Is there a way? Or at least is there a better way than dynamically building query for every keyword:

select * from table where field like "%apple%" or field like "%orange%"

Thanks.

+1  A: 

You probably should look at MySQL's full text indexing, if that is what you're trying to do.

Rowland Shaw
+1  A: 

Maybe a better solution would be to use a boolean search against a fulltext index?

EDIT: I looked it up and it only supports wildcards at the end of words:

ALTER TABLE table ADD FULLTEXT INDEX (field);

SELECT * FROM table WHERE MATCH (field)
AGAINST ('orange* apple*' IN BOOLEAN MODE);
ʞɔıu
+4  A: 

I'm not sure it's any better than what you came up with but you could use MySQL's regex capabilities:

select * from my_table where field rlike 'apple|orange';

Also, as others have mentioned, you could use MySQL's full text search capabilities (but only if you're using the MyISAM engine).

Asaph
Yeah, looks like the easiest approach. Thanks. Hopefully it doesn't affect performance much.
serg
@serg555: I don't think you'll get good performance out of the regex approach because no index will be used. I would recommend the full text search approach.
Asaph
Yeah, but it also has its disadvantages: only myisam tables, only one end wildcards.
serg