這??篇文章主要介紹了M??ySQL 分組查詢(xún)和聚合函數的組查相關(guān)資料,幫助大家更好的詢(xún)和理(li)解和使用MySQL,感興趣的聚合朋友可以了解下
概述
相信我們經(jīng)常會(huì )遇到這樣的場(chǎng)景:想要了解雙十一天貓購買(mǎi)化妝品的人員中平均消費額度是多少(這可能有利于對商品價(jià)格區間的定位);或者不同年齡段的化妝品消費占比是多少(這可能有助于對商品備貨量的預估??)。
這個(gè)??時(shí)候就要用到分組查詢(xún),函數分組查詢(xún)的組查目的是為了把數據分成多個(gè)邏輯組(購買(mǎi)化妝品的??人員是一個(gè)組,不同年齡段購買(mǎi)化妝品的詢(xún)和人員也是組),并對每個(gè)組進(jìn)行聚合計算的聚合過(guò)程:。
分組查詢(xún)的函數語(yǔ)法格式如下:
select cname, group_fun,... from tn??ame [where condition]
group by group_expression [having gr??oup_condition];
說(shuō)明一下:
1、group_fun 代表聚合函數,組查是詢(xún)和指對分組的數據進(jìn)行聚合計算的函數。
2、聚合group_expression 代表分組表達式,函數允許多個(gè),組查多個(gè)之間使用逗號隔開(kāi)。詢(xún)和(he)
3、聚合group_condition(′▽?zhuān)?) 分組之后,???再對分組┐(′?`)┌后的數據進(jìn)行條件過(guò)濾的過(guò)程。
4、分組語(yǔ)法中,select后面出現的字段 要么是group by后面的字段,要么是聚合函數的列,其他類(lèi)型會(huì )報異常,我們下面的內容中會(huì )詳細說(shuō)明。
聚合函數有ヽ(′ー`)ノ以下幾種。
AVG()函數
AVG()通過(guò)對表中行數計數并計算特定列值之和,求得該列的平均值。 AVG()可用來(lái)返回所有列的平均值,也可以用來(lái)返回特定列或行的平均值。
下面示例返回用戶(hù)表中用戶(hù)的平(ping)均年齡:
mysql> select * from user2;
+----+--------+------+-------(╯‵□′)╯---+-----+
| id | name | age?? | address | sex |
+----+--------+------+--??--------+-----+
| 1 | brand | 21 | fuzhou | 1 |
| 2 | helen | 20 | quanzhou | 0 |
| 3 | sol | 21 | xiamen | 0 |
| 4 | weng | 33 | guizhou | 1 |
| 5 | selina | 25?? | NULL | 0 |
| 6 | anny | 23 | sha┐(′?`)┌nghai | 0 |
| 7 | annd | 24 | shanghai | 1 |
| 8 | sunny | NULL | guizhou | 0 |
+--??--+--------+------+----------+-----+
8 rows?? in set
mysql> select avg(age) from user2;
+----------+
| av(╬?益?)g(age) |
+----------+
| 23.8571?? |
+----------+
1 row in set
注意點(diǎn):
2、AVG()函數忽略列值為NULL的行,所以上圖中age值累加之后是除以7,而不是除以8。(?⊿?)
C??OUNT()函數
COUN(′ω`)T()函數進(jìn)行(xing)計數。 可以用COUNT()確定表(biao)中符合條件的行的數??目。
count 有 count(*)、count(具體字段)、count(常量)(′_`) 三種方式來(lái)體現 下面 演示了count(*) 和 count(cname)的用法。
mysql> select * from user2;
+----+--------+------+------??----+-----+
| id | name | age | address | sex |
+----+--------+-┐(′ー`)┌-----+----------+-----+
| 1 | brand | 21 | fuzhou | 1 |
| 2 | helen | 20?? | quanz(??ヮ?)?*:???hou | 0 |
| 3 | sol | 21 | xiamen | 0 |
| 4 | weng | 33?? | guizhou | 1 |
| 5 | selina | 25 | NULL(???) | 0 |
| 6 | anny | 23 | shanghai | 0 |
| 7 | annd | 24 | shanghai | 1 |
| 8 | sunny | NULL | guizhou | 0 |
+----+--------+-----ヽ(′?`)ノ-+----------+-----+
8┐(′д`)┌ rows in set
mysql> select cou(′?ω?`)nt(*) from user2 where sex=0;
+----------+
| count(*) |
+----------+
| 5 |
+----------+
1 roヽ(′ー`)ノw in set
mysql> select?? count(age) from user2 where sex=0;
+------------+
| count(age) |
+------------+
| 4 |
+------------+
1 row in set
可以看到,都是取出女生的用戶(hù)數量,count(*) 比 count(age) 多一個(gè),那是因為age中包含null??值。
所以:如果指定列名,則指定列的值為空的行被COUNT()函數忽略,但如果COUNT()函數中用的是星號( *),則不忽略。
mysql> select * from user2;
+----+--------+------+----------+-----+
| id | name | age | address | sex |
+----+--------+------+----------+-----+
| 1 | brand | 21 | fuzhou | 1 |
| 2 | helen | 20 | quanzhou | 0 |
| 3 | sol | 21 | xiamen | 0 |
| 4 | weng | 33 | guizhou | 1 |
| 5 | selina | 25 | NULL | 0 |
| 6 | ann(╯°□°)╯y | 23 | shanghai | 0 |
| 7 | annd | 24 | shanghai | 1 |
| 8 | sunny | NULL | guizhou | 0 |
+----+--------+------+----------+-----+
8 rows(╥_╥) in set
mysql> select max(age),min(age) from user2;
+----------+----------+
| max(age) | min(age) |
+------(′;д;`)----+----------+
| 33 | 20 |
+----------+----------+
1 row in set
SUM函數
SUM()用來(lái)返回指定(′▽?zhuān)?)列值的和(總計) ,下面返回了所有年齡的總和,同樣的,忽略了null的值
mysql> select * from us(′ω`*)er2;
+----+--( ?ヮ?)------+------+----------+-----+
| id | name | age | addressヾ(′ω`)? | sex |
+-(′_`)---+--------+------+----------+-----+
| 1 | brand | 21 | fuzhou | 1 |
| 2 | helen | 20 | quanzhou | 0 |
| 3 | sol | 21 | xiamen | 0 |
| 4 | weng | 33 | guizhou | 1 |
| 5 | selina | 25 | NULL?? | 0 |
| 6 | anny | 23 | shanghai | 0 |
| 7 | annd | 24 | shanghai | 1 |
| 8 | sunny | NULL | guizhou | 0 |
+----+-------ヾ(′?`)?-+------+----------+-----+
8 rows in set
mysql> select sum(age) from user2;
+----------+
| sum(age) |
+----------+
| 167 |
+----------+
1 row in set
分組查詢(xún)
數據準備,假設我們有一個(gè)訂貨單表如下(記載用戶(hù)的訂單金額(T_T)和下(′▽?zhuān)?單時(shí)間):
mysql&??gt; select * fr??om t_or(╯°□°)╯︵ ┻━┻der;
+---------+-----+-------+--------+---------------------+(′?`)------+??
| orderid | uid | un??ame | amount?? | time | year |
+---------+----(′?`)-+-------+--------+---------------------+------+
| 20 | 1 | brand | 91.23 | 2018-08-20?? 17:22:21 | 20(⊙_⊙)18 |
| 21 | 1 | brand | 87.54 | 2019-07-16 09:(?????)21:30 | 2019 |
| 22 | 1 | brand | 166.88 | 2019(′_ゝ`)-04-04 12:23:55 | 2019 |
| 23 | 2 | helyn | 93.73 | 2019-09-15 10:11:11 | 2019 |
| 24 | 2 | helyn | 102.??32 | 2019-01-08 17?:33??:25 | 2019 |
| 25 | 2 | hely(°ロ°) !n | 106.06 | 2019-12-??24 12:25:2??5 | 2019 |
| 26 | 2 | helyn | 73.42 | 2020-04-03 17:16:2??3 | 2020 |
| 27 | 3 | sol | 55.55 | 2019-08-05 19:16:23 | 2019 |
| 28 | 3 | sol | 69.96 | 2020-09-16 19:23:16 | 2020 |
| 29 | 4 | weng | 199.99 | 2020-06-08 19:55:06 | 2020 |
+---------+-----+-------+--------+---------------------+------+
10 rows in set
單字段分組
即對于某個(gè)字段進(jìn)行分組,比如針對用戶(hù)進(jìn)行分組,輸出他們的用戶(hù)Id,訂單數量和總額:
my(′?_?`)sql> select uid,count(uid),sum(amount) from t_order group by uid;
+-----+------------+-------------+
| uid | count(uid) | sum(amount) |
+-----+------(╯°□°)╯------+-------------+
| 1 | 3 | 345.65 |
| 2 | 4 | 375.53 |
| 3 | 2 | 125.51 |
| 4 | 1 | 199.99 |
+-----+------------+-------------+
4 rows in set
多字段分組(zu)
即對于多個(gè)字段進(jìn)行分組,比如針對用戶(hù)進(jìn)行??分組,再對他們不同年份的訂單數據進(jìn)行分組,輸出訂單數量(liang)和消費總額:
mysql> select uid,count(uid) as nums,sum(amount) as totalamount,year from t_order group by uid,year;
+-----+------+-------------+------+
| uid | nums | totalamount | year |
+-----+------+-------(′?`*)------+------+
| 1 | 1 | 91.23 | 2018 |
| 1 | 2 | 254.42 | 2019 |
| 2 | 3 | 302.11 | 2019 |
| 2 | 1 | 73.42 | 2020 |
| 3 | 1 | 55.55 | 2019 |
|?? 3 | 1 | 69.96 | 2020 |
| 4 | 1 | 199.99 | 2020 |
+-----+------+-------------+------+
7 rows in set
分組前的條件過(guò)濾:where
這個(gè)很簡(jiǎn)單,就是再分組(group by)之前通過(guò)where關(guān)鍵字進(jìn)行??條件過(guò)濾,取出我們需要的數據,假設我(wo)們只要列出2019年8月之后的數據,源數據只有6條??合格的,有兩條(╯°□°)╯年份一樣被分組的:
mysql> select uid,count(uid) as nums,sum(amount) as totalamount,year from t_order where time > '2019-08-01' group by uid,year;
+-----+??------+-----------??--+------+
| uid | nu??ms | totalam??ount | year |
+-----+------+-------------+------+
|ヽ(′▽?zhuān)?ノ 2 | 2 | 199.79 | 2019 |
| 2 | 1 | 73.42 | 2020 |
| 3 | 1 | 55.55 | 2019 |
| 3 | 1 | 69.96 | 2020 |
| 4 | 1 | 199.99 | 2020 |
+-----+------+-------------+------+
5 rows in set
有時(shí)候我們需要再分組之后再對數據??進(jìn)行過(guò)濾,這時(shí)候就需要使用having關(guān)鍵字進(jìn)行數據過(guò)濾,再上述條件下,我們需要取出消費次數超過(guò)一次的數??據:
mysql> select uid,count(uid) as nums,sum(amount) as total??amount,year from t_orde??r where time > '2019-08-01' group by uid,year having nums>1;
+-----+------+-------------+------+
| uid | nums | totalamount | year |
+-----+------+-------------+------+
| 2 | 2 | 199.79 | 2019 |
+-----+------+-------------+------+
1 row in set
這邊需要注意區分wh??ere和ha??ving:
where是在分組(聚合)前對記(?Д?)錄進(jìn)行篩選,而having是在分組結束后的(′?_?`)結果里篩選,最后返回過(guò)濾后的結果。
可以把having理解為兩級查詢(xún),即含having的查詢(xún)操作先獲得不含having子句時(shí)的sql查詢(xún)結ヾ(′▽?zhuān)??果表,然后在這個(gè)結果表上使用having條件篩選出符合(he)的記錄,最后返回這些記錄,因此,having后是可以跟聚合函數的,并且這個(gè)聚集函數不必與select后面的聚集函數相同。
分組后??的排序處理
mysql> select uid,count(uid) as nums,sum(amount) as totalamount from t_order group by uid;
+??-----+------+-------------+
| uid | nums | totalamount |
+-----+------+-------------+
| 1 | 3 | 345ヽ(′?`)ノ.65 |
| 2 | 4 | 375.53 |
| 3 | 2 | 125.51 |
| 4 | 1 | 199.99 |
+-----+------+-------------+
4 rows in set
mysql> select uid,count(uid) as nums,sum(amount) as totalam??ount from t_order group by uid orde??r by totalamount desc;
+-----+------+-------------+
| uid | nums | totalamount |
+--(′?ω?`)-??--+------??(′ω`)+-------------+
| 2 | 4 | 375.53 |
| 1 | 3 | 345.65 |
| 4 | 1 | 199.99 |
| 3 | 2 | 125.5??1 |
+-----+------+------?-------+
4 rows in set
分組后的limit 限制
limit限制關(guān)鍵字一般放在語(yǔ)句的最末尾,比如基于我們上面的搜索,我們再limit 1,只取出消費額最高的那條,其他跳過(guò)。
mysql> select uid,count(uid) as nums,sum(amount) as totalamount from t_order group by uid order by totalamount desc limit 1;
+-----+----??--+-------------+
| uid | nums | totalamount |
+-----+------+-------------+
| 2 | 4 | 375.53 |
+-----+------+-------------+
1 row in setヽ(′?`)ノ
關(guān)鍵字的執行順序
我們看到上面那我們用了 where、group by、having、order by、limit這些關(guān)鍵字,如果一起使用,他們???是有先后順序,順??序錯了(le)會(huì )導致異常,語(yǔ)法格式如下:
select cname from tname
where [原表查詢(xún)條件]
group by [分組表達式]
h( ?ヮ?)aving [分組過(guò)濾條件]
order by [排序條件]
limit [offset,] count;
mysql> select uid,count(uid) as nums,sum(amount) as totalamount from t_order where time > '2019-08-01' group by uid having totalamount>100 order by totalamount desc limit 1;
+-----+------+-------------+
| uid | nums | totalamoun(′?ω?`)t |
+-----+------+-------------+
| 2 | 3 | 273.21 |
+-----+------+-------------+
1 row in set
總結
1、分組語(yǔ)法中,select后面出現的字段 要么是??group by后面的字段,要么是(//ω//)聚ヽ(′▽?zhuān)?ノ合(he)函數的列,其他類(lèi)型會(huì )報異常:可以自己試試??。
2、分組關(guān)鍵字的執ヽ(′▽?zhuān)?ノ行順序:where、group by、??having、order by、limit,順序不能調換,否則會(huì )報異常:可以自己試試。
以上就是MySQL 分組查詢(xún)和聚合函數的詳細內容,更多關(guān)于MySQL 分組查詢(xún)和聚合函數的資料請關(guān)注腳本之家其它相關(guān)文章!??
來(lái)源:腳本之家
鏈接:https://www.jb51.net/article/199??80ヽ(′?`)ノ3.htm
(作者:SEO內容優(yōu)化)