1

我需要在子查询中进行子查询是什么导致“'where 子句'中的未知列't1.product_id'”。它在第 7 行。在我的示例中。如何解决这个问题呢?

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

此问题从GROUP_CONCAT 查询中忽略的 LIMIT继续

4

2 回答 2

4

我发现在具有超过 2 个选择深度的查询中使用变量更舒服。

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
于 2013-04-05T16:54:21.930 回答
3

使用派生表/内联视图和表别名:

  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
于 2010-07-31T17:30:31.173 回答