SQL将多个查询汇总为一个

时间:2014-11-22 07:58:19

标签: mysql sql-server

我有一个关于总结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行结果?

2 个答案:

答案 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