GROUP_CONCAT 不适用于左连接
Posted
技术标签:
【中文标题】GROUP_CONCAT 不适用于左连接【英文标题】:GROUP_CONCAT not working with left join 【发布时间】:2015-05-28 23:48:11 【问题描述】:此查询显示错误: #1054 - 'on 子句'中的未知列 'sp.spot_id'
查询:
SELECT product.*,sp.sp_name FROM `product`
left join spot_selling on product.product_id=spot_selling.product_id
AND spot_selling.end_time >= now()
AND spot_selling.start_time <= now()
AND spot_selling.status='1'
left join(select GROUP_CONCAT(s.name SEPARATOR ',') as sp_name
from spot s group by s.spot_id) sp on sp.spot_id=spot_selling.spot_id
WHERE product.user_id='26' AND product.status!='6'
【问题讨论】:
它是如何“不工作”的? 【参考方案1】:我认为你只需要在子查询中使用spot_id
,所以有一些东西可以加入:
SELECT product.*, sp.sp_name
FROM `product` left join
spot_selling
on product.product_id=spot_selling.product_id AND
spot_selling.end_time >= now() AND
spot_selling.start_time <= now() AND
spot_selling.status = '1' left join
(select spot_id, GROUP_CONCAT(s.name SEPARATOR ',') as sp_name
--------------^
from spot s
group by s.spot_id
) sp
on sp.spot_id = spot_selling.spot_id
WHERE product.user_id = '26' AND product.status <> '6'
【讨论】:
以上是关于GROUP_CONCAT 不适用于左连接的主要内容,如果未能解决你的问题,请参考以下文章
Python / Sqlite3:使用 GROUP_CONCAT 左连接