关于group by 、group by having、where group by与 group by order by

SQL教程:

https://www.w3cschool.cn/sql/

http://www.runoob.com/sql/sql-tutorial.html

 

 

转载:https://blog.csdn.net/qq_28007533/article/details/72859474

现有表 score

name      kecheng     fenshu 
张三     语文       81
张三     数学       75
李四     语文       76
李四     数学       90
王五     语文       81
王五     数学       100
王五     英语       90

group by 的使用

在使用group by 时,有一个规则需要遵守,即出现在select列表中的字段,如果没有在聚合函数中,那么必须出现在group by 子句中。(select中的字段不可以单独出现,必须出现在group语句中或者在组函数中。)

select  a.name,a.fengshu   from score a  group by a.name   这是错误的

select  a.name,min(a.fengshu)  from score a group by a.name  这是可以的

 

group by having的使用

having的筛选是在分组以后,并且针对分组中的每一条数据,只要有一条不符合要求,这一组都不会被筛选出来

筛选出每一门课程分数都大于80的学生姓名

select a.name  from score a group by a.name having min(a.fengshu)>80

 

where group by 

where 的筛选是在分组之前,所以在where 里面可以出现任意字段

select a.namefrom score a

where a.fenshu>80 

group by a.name 

 

group by  ordey by 

select a.fenshu from score a group by a.fenshu order by a.fenshu

 

ps:到底啥时候用group by ?

看select后的字段 能不能成为一个唯一标识,如果不能,则使用group by 可以分组

posted @ 2018-07-27 15:10  SpringMVCMaven  阅读(313)  评论(0编辑  收藏  举报