siri
siri

Reputation: 723

combining LIKE with IN operator in MySQL

I want to do some thing like this:

select * from table1 where name Like in (select name from table2 where id = '%123')

Suppose

select name from table2 where id = '%123' 

Results are: abc123,def123,ghi123,...

And the table1 contains the name fields as AB-abc123-CD,CD-def123-HB,...

How can I do this?

Upvotes: 1

Views: 698

Answers (2)

Albin Sunnanbo
Albin Sunnanbo

Reputation: 47038

This works on SQL Server, I assume it will work on MySQL too, possibly with minor modifications.

select distinct t1.* from table1 t1
inner join table2 t2
on t1.name like '%' + t2.name + '%'
where t2.ShortString like '%123'

Upvotes: 0

Fun Mun Pieng
Fun Mun Pieng

Reputation: 6891

If I understand you correctly:

select * from table1 where name in (select name from table2 where id Like '%123')

OR

select * from table1 inner join table2 on table1.name like '%' || table2.name || '%'
where table2.id = '%123'

Did I understand you question correctly?

Upvotes: 1

Related Questions