我有三张桌子:
SELECT P.`id`, P.`name`, C.`name` as catName, SUM(S.`quantity`)
FROM `products` AS P, `categories` AS C, `sales` AS S
WHERE P.`category_id` = C.`id`
AND P.`id` = S.`id_product`
AND P.`id` IN (
SELECT `id_product` FROM `sales`
WHERE `selled`=1
)
LIMIT 8
提供我想要的所有信息,但数量未按id_product 分组/汇总的事实除外。
所以,为了解决这个问题,尝试使用 SUM(S.quantity)
和分组来执行id_product:
SELECT P.`id`, P.`name`, C.`name` as catName, SUM(S.`quantity`)
FROM `products` AS P, `categories` AS C, `sales` AS S
WHERE P.`category_id` = C.`id`
AND P.`id` = S.`id_product`
AND P.`id` IN (
SELECT `id_product` FROM `sales`
WHERE `selled`=1
)
LIMIT 8
GROUP BY P.`id`
查询后,我收到错误
1064 - 您的SQL语法出错;检查与您的MySQL服务器版本相对应的手册,以便在#BY; GROUP BY P附近使用正确的语法。
id
'在第3行
答案 0 :(得分:1)
GROUP BY来自LIMIT
节日快乐!