分组查询以及having使用

Group  by 注意事项:对查询的列有限制,除了聚合函数外,就必须是分组的项

order  by总是放在最后

代码示例:

select Gender as 性别 ,count (Gender) as 总和 from UserInfo

 GROUP by Gender

备注:

Where和having的区别:

Where是在分组之前的查询,having是在分组之后的进行查询

order  by排序必须在最后

代码示例:

--分组查询 group by

select Gender as 性别, count (Gender) as 总和 from UserInfo

 GROUP by Gender

orderby Gender desc

 

--如何对分组之后的结果进行筛选HAVING

select Gender as 性别,count(Gender) as 总和 from UserInfo

 GROUP by Gender

having count(Gender)>6

order by Gender desc

posted @ 2018-10-29 08:41  锦大大的博客呀!  阅读(1496)  评论(0编辑  收藏  举报