当前位置:Gxlcms > PHP教程 > 请教一个简单的SQL

请教一个简单的SQL

时间:2021-07-01 10:21:17 帮助过:16人阅读

请教一个简单SQL:

select * from eload_goods where goods_sn like '(select left(goods_sn,9) as goods_sn from eload_goods where goods_id = 126300 limit 1)%'
这个SQL有啥错误?搜不到结果.

select * from eload_goods where goods_sn = (select left(goods_sn,9) as goods_sn from eload_goods where goods_id = 126300 limit 1)
这样有搜索是有结果的.

注:子查询是正确的.子查询返回结果为:IE0384701
goods_sn为varchar数据类型


回复讨论(解决方案)

请问加个%是啥意思

没有错
但我绝不相信你的表中有 (select left(goods_sn,9) as goods_sn from eload_goods where goods_id = 126300 limit 1)... 这样的字符串

没有错
但我绝不相信你的表中有 (select left(goods_sn,9) as goods_sn from eload_goods where goods_id = 126300 limit 1)... 这样的字符串

版主,您错了.
(select left(goods_sn,9) as goods_sn from eload_goods where goods_id = 126300 limit 1)是绝对正确的.也有这样的字符串.

问题己解决,附上答案:
SELECT * FROM eload_goods WHERE goods_sn LIKE (SELECT CONCAT(LEFT(goods_sn,9),'%') AS goods_sn FROM eload_goods WHERE goods_id = 126300 LIMIT 1)

我错在哪?
select * from eload_goods where goods_sn like '(select left(goods_sn,9) as goods_sn from eload_goods where goods_id = 126300 limit 1)%'
不知字符串吗

SELECT * FROM eload_goods WHERE goods_sn LIKE (SELECT CONCAT(LEFT(goods_sn,9),'%') AS goods_sn FROM eload_goods WHERE goods_id = 126300 LIMIT 1)

这样的sql不蛋疼吗?

我错在哪?
select * from eload_goods where goods_sn like '(select left(goods_sn,9) as goods_sn from eload_goods where goods_id = 126300 limit 1)%'
不知字符串吗

MYSQL的子查询不支持limit,应该是limit 1导致的错误吧
如果goods_id是主键的话,limit 1完全没有必要,而且不被支持

错误的原因在于单引号,单引号中的内容并没有进行查找,而是直接将引号内容区匹配了

SELECT * FROM eload_goods WHERE goods_sn LIKE (SELECT CONCAT(LEFT(goods_sn,9),'%') AS goods_sn FROM eload_goods WHERE goods_id = 126300 LIMIT 1)

这样的sql不蛋疼吗?

请问如何写更好?

我错在哪?
select * from eload_goods where goods_sn like '(select left(goods_sn,9) as goods_sn from eload_goods where goods_id = 126300 limit 1)%'
不知字符串吗

我理解你的意思,'select...' 是字符串,所以我是请教解决的写法嘛。

人气教程排行