我有以下MySQL代碼:MySQL聚合查詢?
SELECT
c.categoryId,
c.categoryName, c.categoryParent,
c.categoryDescription,
COUNT(p.productid) as totalProdsInCategory
FROM categories as c
LEFT JOIN normalproducts as p
ON c.categoryId = p.categoryid
WHERE c.categoryId = 41
GROUP BY c.categoryId
ORDER BY c.categoryParent ASC, c.categoryName ASC
我希望能夠包括另算列。但是這隻能算作p.state =「active」的產品。這是我的錯誤解決方案
SELECT
c.categoryId,
c.categoryName, c.categoryParent,
c.categoryDescription,
COUNT(p.productid) as totalProdsInCategory,
COUNT(q.productid) as totalActiveProdsInCategory
FROM categories as c
LEFT JOIN normalproducts as p
ON c.categoryId = p.categoryid
WHERE c.categoryId = 41
GROUP BY c.categoryId
ORDER BY c.categoryParent ASC, c.categoryName ASC
任何幫助嗎?我不知道該從哪裏出發......
+1比我的好,但我會離開我的 – 2012-03-29 18:04:03