背景:
在学习到group by分组查询时,涉及到对分组后的表的字段进行筛选,但是不能写在where后面,所以使用了having这个功能。
初步理解where是对from后面的表进行筛选再分组,而having是对分组后的虚拟表再进行筛选。这里面就涉及到where,group by以及having的执行顺序,再加上之前的order by等,确实有点迷惑。所以在网上查询,在此进行记录。
比如一个终极缝合怪。。。:
select distinct s.id from T t join S s on t.id=s.id where t.name="Yrion" group by t.mobile having count(*)>2 order by s.create_time limit 5;
直接说结论:
from>on>join>where>group by>having+聚合函数>select>order by>limit
注意在where中不可以使用聚合函数,聚合函数主要是(min\max\count\sum等函数)
详细的内容有一篇博客很牛,转载自:
https://www.cnblogs.com/wyq178/p/11576065.html