Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use wildcards in "IN" MySQL statement?

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.

like image 536
serg Avatar asked Oct 30 '09 21:10

serg


People also ask

Can you use wildcards in case statements?

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.

In which constraint we can use wild card character?

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 (?)


2 Answers

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).

like image 115
Asaph Avatar answered Sep 28 '22 10:09

Asaph


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

like image 38
Rowland Shaw Avatar answered Sep 28 '22 10:09

Rowland Shaw