![](/img/trans.png)
[英]Get category count for a category where only the child categories have products
[英]How get values categories an count products for each category in one query?
桌子Category
:
id name
1 test1
2 test2
3 test3
4 test4
5 test5
餐桌Products
:
CategoryId name
1 product1
1 product2
1 product3
1 product4
3 product5
3 product6
3 product7
5 product8
5 product9
對於獲取名稱類別,我們使用:
SELECT Name FROM Category
但是,如何使用left join
命令從此查詢中的表Category
中的表Category
中的表Products
獲取產品的計數產品呢?
您可以使用
SELECT category.name, COUNT(category_id)
FROM category LEFT JOIN product ON category.id = product.category_id
GROUP BY id
和輸出將是
test1 4
test2 0
test3 3
test4 0
test4 2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.