代码之家  ›  专栏  ›  技术社区  ›  Māris Kiseļovs

“未知列”,因为子查询中的子查询

  •  1
  • Māris Kiseļovs  · 技术社区  · 14 年前

    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
    

    LIMIT ignored in query with GROUP_CONCAT

    2 回复  |  直到 7 年前
        1
  •  3
  •   OMG Ponies    14 年前

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

      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
    
        2
  •  4
  •   Adam Fowler    11 年前

    我发现在深度超过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