我正在尝试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)


谢谢你的帮助。

最佳答案

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函数。

关于mysql - CONCAT周围的GROUP_CONCAT给出错误-组功能的无效使用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14661723/

10-12 23:54