0

我正在尝试对以下查询返回的结果进行 GROUP_CONCAT:

SELECT
      CONCAT(p.product_name, " (", SUM(bp.quantity), ")") AS products
FROM product p
INNER JOIN booking_products bp ON p.product_id = bp.product_id
GROUP BY p.product_id

此查询返回以下内容:

------------
| products |
------------
Shampoo (3)
Advil (1)
Dry Shampoo (4)
Cepacol (5)

当我尝试运行下面的查询时,我收到一个错误:“#1111 - 组函数的使用无效”。这有什么问题?

SELECT
    GROUP_CONCAT(
        CONCAT(p.product_name, " (", SUM(bp.quantity), ")") 
        SEPARATOR ", "
    ) AS products
FROM product p
INNER JOIN booking_products bp ON p.product_id = bp.product_i
GROUP BY p.product_id

当然它应该返回:

Shampoo (3), Advil (1), Dry Shampoo (4), Cepacol (5)

谢谢你的帮助。

4

1 回答 1

7
SELECT GROUP_CONCAT(products) FROM
(
    SELECT CONCAT(p.product_name, " (", SUM(bp.quantity), ")") AS products
    FROM product p
    INNER JOIN booking_products bp ON p.product_id = bp.product_id
    GROUP BY p.product_id
) x

GROUP_CONCAT 不起作用,因为您已经使用了 SUM,它是一个 GROUP 函数。

于 2013-02-02T12:11:22.980 回答