题解 | #返回订单数量总和不小于100的所有订单的订单号#

返回订单数量总和不小于100的所有订单的订单号

http://www.nowcoder.com/practice/ff77e82b59544a15987324e19488aafd

效率优化:

SQL语句书写顺序:
select->distinct->from->join->on->where->group by->having->order by->limit
SQL语句执行顺序
from->on->join->where->group by(开始使用select中的别名,后面的语句中都可以使用别名)->sum、count、max、avg->having->select->distinct->order by->limit
原文链接:https://blog.csdn.net/luohefu1/article/details/122534905

先写where再group by 比用group by再having筛选效率更高

写法1:
select order_num
from OrderItems
group by order_num
having sum(quatity)>=100
order by order_num
写法2:
select order_num
from OrderItems
where quantity >= 100
group by order_num
order by order_num

写法2的查询效率要高于写法1
全部评论
写法2效率更高,但是写法2没有考虑 quantity 的求和,如果一个 order_num 对应不止1个 quantity 就会不对了。
10 回复 分享
发布于 2022-09-18 21:13 上海

相关推荐

点赞 评论 收藏
分享
群星之怒:不是哥们,你就不好奇瘫痪三十年的老植物人是啥样的吗?
点赞 评论 收藏
分享
评论
9
1
分享

创作者周榜

更多
牛客网
牛客企业服务