MySQL資料庫基本用法-彙總-分組

來源:互聯網
上載者:User

標籤:gen   資料庫   平均值   sde   欄位   均值   最大值   結果   color   

彙總
  • 為了快速得到統計資料,提供了5個彙總函式
  • count(*)表示計算總行數,括弧中寫星與列名,結果是相同的
  • 查詢學生總數
select count(*) from students;

  

  • max(列)表示求此列的最大值
  • 查詢女生的編號最大值
select max(id) from students where gender=0;

  

  • min(列)表示求此列的最小值
  • 查詢未刪除的學生最小編號
select min(id) from students where isdelete=0;

 

  • sum(列)表示求此列的和
  • 查詢男生的編號之後
select sum(id) from students where gender=1;

  

  • avg(列)表示求此列的平均值
  • 查詢未刪除女生的編號平均值
select sum(id) from students where gender=1;

  

分組
  • 按照欄位分組,表示此欄位相同的資料會被放到一個組中
  • 分組後,只能查詢出相同的資料列,對於有差異的資料列無法出現在結果集中
  • 可以對分組後的資料進行統計,做彙總運算
  • 文法
select 列1,列2,彙總... from 表名 group by 列1,列2,列3....
  •   查詢男女總數
select gender as 性別,count(*)from studentsgroup by gender;

  查詢各城市人數

 

MySQL資料庫基本用法-彙總-分組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.