我有一个关于总结SQL查询的问题:
SELECT COUNT(fquantity) from items where fcategory="Crab"
SELECT COUNT(fquantity) from items where fcategory="Chicken"
SELECT COUNT(fquantity) from items where fcategory="Beancurd"
SELECT COUNT(fquantity) from items where fcategory="Pork"
SELECT COUNT(fquantity) from items where fcategory="Fish"
SELECT COUNT(fquantity) from items where fcategory="Premium"
SELECT COUNT(fquantity) from items where fcategory="Vegetable"
SELECT COUNT(fquantity) from items where fcategory="Prawn"
有没有办法将它们汇总到单个SQL查询中,该查询将返回包含2列数据(fcategory,count)的8行结果?
答案 0 :(得分:0)
SELECT fcategory,count(fquantity) AS qty FROM items GROUP BY fcategory
答案 1 :(得分:0)
使用group by
按类别按类别对数据进行分组:
<强>查询强>
Select count(fquantity),
fcategory
from items
group by fcategory