我有个需求,需要在MYSQL下like查询另一条sql查询的结果,大概格式如下:
select * from table1 where `text` like '%(select name from table2 where id =3)%';
请问如何才是正确格式?谢谢
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单独占一行)
最佳答案
SELECT *
FROM table1
WHERE `text`
LIKE CONCAT( '%', (SELECT NAME FROM table2 WHERE id = 3 ), '%' );
这样看看,行么 ? CONCAT 是 mysql 中函数, 用于连接字符串的。
本文详细介绍了在MySQL中如何正确使用Like子句来查询包含另一条SQL查询结果的数据。给出了正确的SQL语句示例,并列举了多种错误写法以供参考。

2923

被折叠的 条评论
为什么被折叠?



