码迷,mamicode.com
首页 > 其他好文 > 详细

where, group by, having

时间:2014-07-07 16:58:32      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:cti   io   sql   re   rom   c   

where vs having 当一个sql语句中存在where子句,会先执行where,然后执行group by,然后执行having. 一般来说,only use ‘having‘ when you use ‘group by‘ Always use ‘having‘ with aggregate function. 下面的例子,虽然执行后的结果集一样,但是执行的步骤不同,前者先filter,再group by,后者先group by,然后filter,显然前者更好,而后者浪费了计算资源。 SELECT state, COUNT(*) FROM Test WHERE state IN (‘CA‘, ‘LA‘) GROUP BY state ORDER BY state SELECT state, COUNT(*) FROM Test GROUP BY state HAVING state IN (‘CA‘, ‘LA‘) ORDER BY state

where, group by, having,布布扣,bubuko.com

where, group by, having

标签:cti   io   sql   re   rom   c   

原文地址:http://www.cnblogs.com/Antech/p/3815010.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!