serg
serg

Reputation: 111325

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.

Upvotes: 18

Views: 19184

Answers (4)

Eric Schwa Pivnik
Eric Schwa Pivnik

Reputation: 1

In Oracle, you can do:

select * from table where
regexp_like (column, 'apple|orange', 'i')

You can use more complex regexp. The 'i' makes it insensitive. See Oracle docs

Upvotes: -2

Asaph
Asaph

Reputation: 162851

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

Upvotes: 21

ʞɔıu
ʞɔıu

Reputation: 48446

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

Upvotes: 2

Rowland Shaw
Rowland Shaw

Reputation: 38129

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

Upvotes: 1

Related Questions