Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using SQL LIKE and IN together

Tags:

mysql

Is there a way to use LIKE and IN together?

I want to achieve something like this.

SELECT * FROM tablename WHERE column IN ('M510%', 'M615%', 'M515%', 'M612%'); 

So basically I want to be able to match the column with a bunch of different strings. Is there another way to do this with one query or will I have to loop over the array of strings I am looking for?

like image 887
Nick Avatar asked Feb 23 '10 12:02

Nick


People also ask

Can I use like and in together SQL?

Is there as way to combine the "in" and "like" operators in Oracle SQL? Answer: There is no direct was to combine a like with an IN statement. However Oracle does support several alternative clauses: CONTAINS clause: the contains clause within context indexes.

How use like with in clause in SQL?

The SQL LIKE OperatorThe LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign (%) represents zero, one, or multiple characters. The underscore sign (_) represents one, single character.

Can I use multiple like statements in SQL?

Not only LIKE, but you can also use multiple NOT LIKE conditions in SQL. You will get records of matching/non-matching characters with the LIKE – this you can achieve by percentile (%) wildcard character. Below use cases, help you know how to use single and multiple like conditions.

How do I use multiple wildcards in SQL?

The SQL LIKE clause is used to compare a value to similar values using wildcard operators. There are two wildcards used in conjunction with the LIKE operator. The percent sign represents zero, one or multiple characters. The underscore represents a single number or character.


1 Answers

How about using a substring with IN.

select * from tablename where substring(column,1,4) IN ('M510','M615','M515','M612') 
like image 161
tvanfosson Avatar answered Sep 26 '22 03:09

tvanfosson