选择表LIKE其他表数据的数据

时间:2017-02-25 06:30:36

标签: php mysql

我有两张表,例如。 atabbtab使用mysql查询:
请帮我看下面这个插图(我在mysql中相对较新):

SELECT * FROM atab WHERE column1 LIKE(表column2中的btab);

由于select查询(第2个表)没有指定为单行而是指定LIKE(整行),因此(可能)显示数据需要太长时间,对吧? 如何优化呢?有可能吗?

2 个答案:

答案 0 :(得分:3)

尝试此查询

SELECT * FROM atab WHERE column1 LIKE (select column2 from btab); 

答案 1 :(得分:2)

使用EXISTS

select *
from atab a
where exists (
        select 1
        from btab b
        where a.column1 like concat('%',b.column2,'%')
        );