[MySQL 查詢語句]——分組查詢group by

來源:互聯網
上載者:User

標籤:

#group by
(1) group by的含義:將查詢結果按照1個或多個欄位進行分組,欄位值相同的為一組
(2) group by可用於單個欄位分組,也可用於多個欄位分組

select * from employee;+------+------+--------+------+------+-------------+| num  | d_id | name   | age  | sex  | homeaddr    |+------+------+--------+------+------+-------------+|    1 | 1001 | 張三   |   26 | 男   | beijinghdq  ||    2 | 1002 | 李四   |   24 | 女   | beijingcpq  ||    3 | 1003 | 王五   |   25 | 男   | changshaylq ||    4 | 1004 | Aric   |   15 | 男   | England     |+------+------+--------+------+------+-------------+
select * from employee group by d_id,sex;
select
* from employee group by sex;+------+------+--------+------+------+------------+| num | d_id | name | age | sex | homeaddr |+------+------+--------+------+------+------------+| 2 | 1002 | 李四 | 24 | 女 | beijingcpq || 1 | 1001 | 張三 | 26 | 男 | beijinghdq |+------+------+--------+------+------+------------+根據sex欄位來分組,sex欄位的全部值只有兩個(‘男‘和‘女‘),所以分為了兩組當group by單獨使用時,只顯示出每組的第一條記錄所以group by單獨使用時的實際意義不大

 

 

#group by + group_concat()
(1) group_concat(欄位名)可以作為一個輸出欄位來使用,
(2) 表示分組之後,根據分組結果,使用group_concat()來放置每一組的某欄位的值的集合

select sex from employee group by sex;+------+| sex  |+------+| 女   || 男   |+------+select sex,group_concat(name) from employee group by sex;+------+--------------------+| sex  | group_concat(name) |+------+--------------------+| 女   | 李四               || 男   | 張三,王五,Aric     |+------+--------------------+select sex,group_concat(d_id) from employee group by sex;+------+--------------------+| sex  | group_concat(d_id) |+------+--------------------+| 女   | 1002               || 男   | 1001,1003,1004     |+------+--------------------+

 

 

#group by + 集合函數
(1) 通過group_concat()的啟發,我們既然可以統計出每個分組的某欄位的值的集合,那麼我們也可以通過集合函數來對這個"值的集合"做一些操作

select sex,group_concat(age) from employee group by sex;+------+-------------------+| sex  | group_concat(age) |+------+-------------------+| 女   | 24                || 男   | 26,25,15          |+------+-------------------+
分別統計性別為男/女的人年齡平均值select sex,avg(age) from employee group by sex;+------+----------+| sex | avg(age) |+------+----------+| 女 | 24.0000 || 男 | 22.0000 |+------+----------+
分別統計性別為男/女的人的個數select sex,count(sex) from employee group by sex;+------+------------+| sex | count(sex) |+------+------------+| 女 | 1 || 男 | 3 |+------+------------+

 

 

#group by + having
(1) having 條件運算式:用來分組查詢後指定一些條件來輸出查詢結果
(2) having作用和where一樣,但having只能用於group by

select sex,count(sex) from employee group by sex having count(sex)>2;+------+------------+| sex  | count(sex) |+------+------------+| 男   |          3 |+------+------------+

 

 

#group by + with rollup
(1) with rollup的作用是:在最後新增一行,來記錄當前列裡所有記錄的總和

select sex,count(age) from employee group by sex with rollup;+------+------------+| sex  | count(age) |+------+------------+| 女   |          1 || 男   |          3 || NULL |          4 |+------+------------+select sex,group_concat(age) from employee group by sex with rollup;+------+-------------------+| sex  | group_concat(age) |+------+-------------------+| 女   | 24                || 男   | 26,25,15          || NULL | 24,26,25,15       |+------+-------------------+

 



 

[MySQL 查詢語句]——分組查詢group by

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.