4

我有以下查询:

SELECT products_categories.categoryID, name, COUNT(*) AS itemCount
FROM products_categories
LEFT JOIN products_to_categories ON products_to_categories.categoryID = products_categories.categoryID
GROUP BY products_categories.categoryID

但是仍然存在一个问题:其中没有产品的类别返回itemCount = 1而不是0. 我怎样才能解决这个问题?

4

4 回答 4

4

你试过COUNT(products_to_categories.categoryID) AS itemCount吗?我不太确定,但会认为问题可能出在COUNT(*).

于 2010-08-15T07:48:22.863 回答
4

尝试COUNT(product_name)或其他方式,而不是COUNT(*).

于 2010-08-15T07:48:59.877 回答
1
COUNT(products_to_categories.categoryID)

要求COUNT(*)至少给你 1,因为毕竟有1行。具体计数需要具体处理。

于 2010-08-15T07:47:48.417 回答
-1
SELECT products_categories.categoryID, name, COUNT(*) AS itemCount
FROM products_categories
LEFT JOIN products_to_categories ON products_to_categories.categoryID = products_categories.categoryID
GROUP BY products_categories.categoryID
WHERE itemCount <> '0'
于 2010-08-15T07:48:23.780 回答