03-第 13 章 分组数据
2021-09-24 本文已影响0人
紫荆秋雪_文
一、分组+过滤+排序
检索总计订单价格大于等于 50 的订单的订单号和总计订单价格
SELECT order_num, SUM(quantity*item_price) AS orderTotal
FROM orderItems
GROUP BY order_num
HAVING SUM(quantity*item_price) >= 50
ORDER BY orderTotal
- GROUP BY 子句用来按订单号(order_num)分组数据,以便SUM(*)函数能够返回总计订单价格
- HAVING 子句过滤数据,使得只返回总计订单价格大于等于 50 的订单
- ORDER BY 子句排序输出
二、SELECT 子句顺序
image.pngimage.png