2011-10-11 45 views
1

LIKE与运营商结合我想要做一些事情是这样的:在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,...

我该怎么办? S'

回答

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' 

我才明白你正确的问题吗?

+0

是的,它工作完美。 – siri

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'