Table Category
:
id name
1 test1
2 test2
3 test3
4 test4
5 test5
Table Products
:
CategoryId name
1 product1
1 product2
1 product3
1 product4
3 product5
3 product6
3 product7
5 product8
5 product9
For get names category we use:
SELECT Name FROM Category
But how get count products from table Products
for each category from table Category
in this query with command left join
?
you can use
SELECT category.name, COUNT(category_id)
FROM category LEFT JOIN product ON category.id = product.category_id
GROUP BY id
and the output will be
test1 4
test2 0
test3 3
test4 0
test4 2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.