SQL的GROUP BY用法小结
利用聚合函数进行分组
使用COUNT()
、AVG()
、MIN()
、MAX()
等聚合函数可实现对分组的过滤,聚合函数会分别对各组数据进行聚合。
以下面的orders表为例:
按照status列将订单分组并计算各组包含的订单条目数:
SELECT status, COUNT(status)
FROM orders
GROUP BY status;
查询结果为:
再以一个orderdetails表为例:
若希望计算每个订单中包含商品的总价,则有
SELECT
orderNumber,
SUM(quantityOrdered * priceEach) AS amount
FROM orderdetails
GROUP BY orderNumber;
查询结果为:
利用表达式进行分组
GROUP BY
语句中还可以使用函数或表达式来处理数据。
例如,从上述orders表和orderdetails表中查询每年订单总额:
SELECT
YEAR(orderDate) AS year,
SUM(quantityOrdered * priceEach) AS total
FROM orders
INNER JOIN orderdetails USING (orderNumber)
GROUP BY YEAR(orderDate);
查询结果为:
使用HAVING语句对分组结果进行过滤
HAVING
语句可以过滤出分组后满足特定条件的数据。
例如,查找出总额大于60000的订单
SELECT
orderNumber,
SUM(quantityOrdered * priceEach) AS total
FROM orderdetails
GROUP BY orderNumber
HAVING total > 60000;
查询结果为:
注意:
WHERE
语句和HAVING
语句虽然都可以对数据进行过滤,但用法是不同的,HAVING
语句可以用于过滤分组,WHERE
语句只能过滤行,不能过滤分组;因此WHERE
语句应在GROUP BY
之前使用。例如:
SELECT
orderNumber,
SUM(quantityOrdered * priceEach) AS total
FROM orderdetails
WHERE quantityOrdered > 50 AND priceEach > 100
GROUP BY orderNumber;
查询结果为:
且WHERE
语句中不能使用别名或者表达式,例如下列写法是不合法的:
SELECT
orderNumber AS orderId
FROM orderdetails
WHERE orderId > 100100
GROUP BY orderNumber;
--> Error Code: 1054. Unknown column 'orderId' in 'where clause'
SELECT
orderNumber,
SUM(quantityOrdered * priceEach) AS total
FROM orderdetails
WHERE SUM(quantityOrdered * priceEach) > 60000
GROUP BY orderNumber;
--> Error Code: 1111. Invalid use of group function 0.000 sec