在MySQL中将LIKE与IN运算符相结合

时间:2011-10-11 04:40:03

标签: mysql

我想做一些这样的事情:

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

假设

select name from table2 where id = '%123' 

结果是:abc123,def123,ghi123,...

table1包含名称字段AB-abc123-CD,CD-def123-HB,...

我该怎么做?

2 个答案:

答案 0 :(得分:1)

如果我理解正确的话:

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'

我是否理解你的问题?

答案 1 :(得分:0)

这适用于SQL Server,我认为它也适用于MySQL,可能只需稍作修改。

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