函數(shù) | 說明 |
AVG() | 返回指定字段的平均值 |
COUNT() | 返回查詢結(jié)果行數(shù) |
MAX() | 返回指定字段的最大值 |
MIN() | 返回指定字段的最小值 |
SUM() | 返回指定字段的求和值 |
AVG()函數(shù)
AVG()通過對(duì)表中行數(shù)計(jì)數(shù)并計(jì)算特定列值之和,求得該列的平均值。 AVG()可用來返回所有列的平均值,也可以用來返回特定列或行的平均值。
下面示例返回用戶表中用戶的平均年齡:
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 | shanghai | 0 | | 7 | annd | 24 | shanghai | 1 | | 8 | sunny | NULL | guizhou | 0 | +----+--------+------+----------+-----+ 8 rows in set mysql> select avg(age) from user2; +----------+ | avg(age) | +----------+ | 23.8571 | +----------+ 1 row in set
注意點(diǎn):
1、AVG()只能用來確定特定數(shù)值列的平均值 。
2、AVG()函數(shù)忽略列值為NULL的行,所以上圖中age值累加之后是除以7,而不是除以8。
COUNT()函數(shù)
COUNT()函數(shù)進(jìn)行計(jì)數(shù)。 可以用COUNT()確定表中符合條件的行的數(shù)目。
count 有 count(*)、count(具體字段)、count(常量) 三種方式來體現(xiàn) 下面 演示了count(*) 和 count(cname)的用法。
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 | shanghai | 0 | | 7 | annd | 24 | shanghai | 1 | | 8 | sunny | NULL | guizhou | 0 | +----+--------+------+----------+-----+ 8 rows in set mysql> select count(*) from user2 where sex=0; +----------+ | count(*) | +----------+ | 5 | +----------+ 1 row in set mysql> select count(age) from user2 where sex=0; +------------+ | count(age) | +------------+ | 4 | +------------+ 1 row in set
可以看到,都是取出女生的用戶數(shù)量,count(*) 比 count(age) 多一個(gè),那是因?yàn)閍ge中包含null值。
所以:如果指定列名,則指定列的值為空的行被COUNT()函數(shù)忽略,但如果COUNT()函數(shù)中用的是星號(hào)( *),則不忽略。
MAX()和MIN()函數(shù)
MAX()返回指定列中的最大值,MIN()返回指定列中的最小值。
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 | 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
注意:同樣的,MAX()、MIN()函數(shù)忽略列值為NULL的行。
SUM函數(shù)
SUM()用來返回指定列值的和(總計(jì)) ,下面返回了所有年齡的總和,同樣的,忽略了null的值
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 | 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
分組查詢
數(shù)據(jù)準(zhǔn)備,假設(shè)我們有一個(gè)訂貨單表如下(記載用戶的訂單金額和下單時(shí)間):
mysql> select * from t_order; +---------+-----+-------+--------+---------------------+------+ | orderid | uid | uname | amount | time | year | +---------+-----+-------+--------+---------------------+------+ | 20 | 1 | brand | 91.23 | 2018-08-20 17:22:21 | 2018 | | 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 | helyn | 106.06 | 2019-12-24 12:25:25 | 2019 | | 26 | 2 | helyn | 73.42 | 2020-04-03 17:16:23 | 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
單字段分組
即對(duì)于某個(gè)字段進(jìn)行分組,比如針對(duì)用戶進(jìn)行分組,輸出他們的用戶Id,訂單數(shù)量和總額:
mysql> 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
多字段分組
即對(duì)于多個(gè)字段進(jìn)行分組,比如針對(duì)用戶進(jìn)行分組,再對(duì)他們不同年份的訂單數(shù)據(jù)進(jìn)行分組,輸出訂單數(shù)量和消費(fèi)總額:
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
分組前的條件過濾:where
這個(gè)很簡單,就是再分組(group by)之前通過where關(guān)鍵字進(jìn)行條件過濾,取出我們需要的數(shù)據(jù),假設(shè)我們只要列出2019年8月之后的數(shù)據(jù),源數(shù)據(jù)只有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 | nums | totalamount | year | +-----+------+-------------+------+ | 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
分組后的條件過濾:having
有時(shí)候我們需要再分組之后再對(duì)數(shù)據(jù)進(jìn)行過濾,這時(shí)候就需要使用having關(guān)鍵字進(jìn)行數(shù)據(jù)過濾,再上述條件下,我們需要取出消費(fèi)次數(shù)超過一次的數(shù)據(jù):
mysql> select uid,count(uid) as nums,sum(amount) as totalamount,year from t_order 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
這邊需要注意區(qū)分where和having:
where是在分組(聚合)前對(duì)記錄進(jìn)行篩選,而having是在分組結(jié)束后的結(jié)果里篩選,最后返回過濾后的結(jié)果。
可以把having理解為兩級(jí)查詢,即含having的查詢操作先獲得不含having子句時(shí)的sql查詢結(jié)果表,然后在這個(gè)結(jié)果表上使用having條件篩選出符合的記錄,最后返回這些記錄,因此,having后是可以跟聚合函數(shù)的,并且這個(gè)聚集函數(shù)不必與select后面的聚集函數(shù)相同。
分組后的排序處理
order條件接在group by后面,也就是統(tǒng)計(jì)出每個(gè)用戶的消費(fèi)總額和消費(fèi)次數(shù)后,對(duì)用戶的消費(fèi)總額進(jìn)行降序排序的過程。
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 totalamount from t_order group by uid order by totalamount desc; +-----+------+-------------+ | uid | nums | totalamount | +-----+------+-------------+ | 2 | 4 | 375.53 | | 1 | 3 | 345.65 | | 4 | 1 | 199.99 | | 3 | 2 | 125.51 | +-----+------+-------------+ 4 rows in set
分組后的limit 限制
limit限制關(guān)鍵字一般放在語句的最末尾,比如基于我們上面的搜索,我們再limit 1,只取出消費(fèi)額最高的那條,其他跳過。
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)鍵字的執(zhí)行順序
我們看到上面那我們用了 where、group by、having、order by、limit這些關(guān)鍵字,如果一起使用,他們是有先后順序,順序錯(cuò)了會(huì)導(dǎo)致異常,語法格式如下:
select cname from tname where [原表查詢條件] group by [分組表達(dá)式] having [分組過濾條件] 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 | totalamount | +-----+------+-------------+ | 2 | 3 | 273.21 | +-----+------+-------------+ 1 row in set
總結(jié)
1、分組語法中,select后面出現(xiàn)的字段 要么是group by后面的字段,要么是聚合函數(shù)的列,其他類型會(huì)報(bào)異常:可以自己試試。
2、分組關(guān)鍵字的執(zhí)行順序:where、group by、having、order by、limit,順序不能調(diào)換,否則會(huì)報(bào)異常:可以自己試試。
以上就是MySQL 分組查詢和聚合函數(shù)的詳細(xì)內(nèi)容,更多關(guān)于MySQL 分組查詢和聚合函數(shù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
標(biāo)簽:沈陽 呼和浩特 阿里 天津 合肥 牡丹江 惠州 公主嶺
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MySQL 分組查詢和聚合函數(shù)》,本文關(guān)鍵詞 MySQL,分組,查詢,和,聚合,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。