我有个需求,需要在MYSQL下like查询另一条sql查询的结果,大概格式如下:
select * from table1 where `text` like '%(select name from table2 where id =3)%';
请问如何才是正确格式?谢谢
正确答案已出:使用CONCAT
SQL语句为:select * from table1 where `text` like CONCAT('%',(select name from table2 where id =3),'%');
感谢没有留名的热心网友,我看在网上问这个问题的不少,但都没有正确答案
************************************
感谢大家的热心,不过请大家在回答前最好自己试下能否在MYSQL下通过
我总结下几种不能达到效果的SQL
1:select * from table1 where `text` like (select name from table2 where id =3);
2:select * from table1 where `text` like '%'(select name from table2 where id =3)'%';
3:select * from table1 where `text` like '%' + (select name from table2 where id =3) +'%';
4:select * from table1 where `text` like '%(select name from table2 where id =3)%';
5:select * from table1 where `text` like '%’select name from table2 where id =3‘%';
6:select * from table1 where `text` like '%(
select name from table2 where id =3
) +'%';(第二条SQL单独占一行)
你这样修改后,就不是我需要的SQL了,因为我并不是完全匹配,而是局部匹配
比如,按照我的理想设计,table1的text值为“我爱家”,从tabel2查询到的name结果为"爱",那么按照你的方式,就查询不出来了
select * from table1 where text like concat('%',(select name from table2 where id =3),'%');
这样子就对了。呵呵。你试试
我在提问前,已经在网上查了资料,你这种方式我已经试过了,是不行的,会直接报错,我也不清楚怎么回事,你如果本机有MYSQL环境,建议试一下,应该也是不行的