mysql 學習筆記05 統計函數的相關使用

  1. 合計函數count, 統計多少條記錄
    在這裏插入圖片描述
統計共有多少學生
select count(*) from students;
查詢數學成績大於等於90的學生數量
select count(*) from students where math >= 90;
查詢總分超過235分的學生的數量
select count(*) from students where (English + math + China) >= 90;

注意:count(*)與count(某個字段)的區別:
在這裏插入圖片描述
2. 合計函數 sum , 計算數值的和
在這裏插入圖片描述

統計一個學生表, 數學總分
select sum(math) as 'mathTotalScore' from students;
統計一個學生表, 數學總分,語文總分,英語總分
select sum(math) as 'mathTotalScore', sum(China) as 'ChinaTotalScore', sum(English) as 'EnglishTotalScore' from students;
統計 數學英語語文,三個科目的成績總和
select sum(math) + sum(English) + sum(China) from students; (推薦)
select sum(math+English+China) from students;(不推薦,這樣只有某個學生,的一科成績爲null,這個學生的成績就統計不進去)
統計一個班的語文平均分
select round(sum(China)/count(name) ,2) as '語文平均分' from students;
  1. 合計函數 avg,返回滿足where條件的一列的平均值
    在這裏插入圖片描述
求一個學生表的數學平均分
select avg(math) as 'avgmath' from students;  如果有9個人,其中一個人math爲null, 那麼這個語句實際上,是 計算的數學總分/8
儘量讓自己的數據爲not null
求一個班級的總分平均
select avg(math + English + China) as 'avgFor_totalScore' from students;
  1. 合計函數max 與 min
    在這裏插入圖片描述
求一個班級 最高分 與 最低分
select max(math + English + China) , min(math + English + China)  from  students;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章