先按字段b 分组,然后在每个分组内按字段a 进行分组:

select * from tablename group by a, b 

如:

我们下面再接着要求统计出每门学科每个学期有多少人选择,应用如下SQL:

 
SELECT Subject, Semester, Count(*)
FROM Subject_Selection
GROUP BY  Semester, Subject


又如:
现在我们想统计每个用户在每种商品上总共花了多少钱,执行如下SQL
 
SELECT Product,Buyer, SUM(Spending)
FROM `Order`
GROUP BY Product, Buyer