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 に答える