關於group by 、group by having、where group by與 group by order by
轉載: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 可以分組
關於group by 、group by having、where group by與 group by order by