开发者

"Unknown column" because of subquery-in-subquery

I need to do subquery in subquery what causes "Unknown column 't1.product_id' in 'where clause'". It's on line 7. in my example. How to solve this problem?

SELECT *,product_id id,
    (SELECT GROUP_CONCAT (value ORDER By `order` ASC SEPARATOR ', '开发者_开发百科) 
    FROM (
        SELECT `order`,value 
        FROM slud_data 
        LEFT JOIN slud_types ON slud_types.type_id=slud_data.type_id 
        WHERE slud_data.product_id = t1.product_id 
          AND value!='' AND display=0 
        LIMIT 3
    ) tmp) text
FROM slud_products t1 
WHERE 
    now() < DATE_ADD(date,INTERVAL +ttl DAY) AND activated=1
    ORDER BY t1.date DESC

This question continues from LIMIT ignored in query with GROUP_CONCAT


I have found it more comfortable to use variables in Queries with More than 2 Selects Deep.

SELECT *,@product := product_id id,
    (SELECT GROUP_CONCAT (value ORDER By `order` ASC SEPARATOR ', ') 
    FROM (
        SELECT `order`,value 
        FROM slud_data 
        LEFT JOIN slud_types ON slud_types.type_id=slud_data.type_id 
        WHERE slud_data.product_id = @product
          AND value!='' AND display=0 
        LIMIT 3
    ) tmp) text
FROM slud_products t1 
WHERE 
    now() < DATE_ADD(date,INTERVAL +ttl DAY) AND activated=1
    ORDER BY t1.date DESC


Use a derived table/inline view, and table aliases:

  SELECT product_id AS id,
         GROUP_CONCAT (y.value ORDER BY y.`order`) 
    FROM slud_products t1 
    JOIN (SELECT sd.product_id, 
                 sd.value,
                 sd.`order`
            FROM SLUD_DATA sd 
       LEFT JOIN slud_types ON slud_types.type_id = slud_data.type_id 
           WHERE value! = '' 
             AND display = 0) y ON y.product_id = t1.product_id 
                               AND y.order <= 3
   WHERE now() < DATE_ADD(date,INTERVAL +ttl DAY) 
     AND activated = 1
GROUP BY product_id
ORDER BY t1.date DESC
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜