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.
A case statement (Section 35.10) is good at string pattern matching. Its "wildcard" pattern-matching metacharacters work like the filename wildcards (Section 1.13) in the shell, with a few twists.
You can use the wildcard characters asterisk (*) and question mark (?) in constraint statements as follows: The asterisk (*) represents any string of zero or more characters. The question mark (?)
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).
You probably should look at MySQL's full text indexing, if that is what you're trying to do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With