题解 | #返回订单数量总和不小于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
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