GROUP_CONCAT无法使用左连接

时间:2015-03-25 10:43:26

标签: mysql left-join group-concat

此查询显示错误:  #1054 - 'on clause'

中的未知列'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 个答案:

答案 0 :(得分:0)

我认为您在子查询中只需要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'