MySQL數(shù)據(jù)查詢之:使用集合/聚合函數(shù)查詢

MySQL數(shù)據(jù)查詢之:使用集合/聚合函數(shù)查詢

mysql聚合函數(shù)如下:

函數(shù) 作用
avg() 返回某列的平均值
count() 返回某列的行數(shù)
max() 返回某列的最大值
min() 返回某列的最小值
sum() 返回某列值的和

(1)count()函數(shù)
(2)sum()函數(shù)
(3)avg()函數(shù)
(4)max()函數(shù)
(5)min()函數(shù)


(免費(fèi)學(xué)習(xí)推薦:mysql視頻教程)

(1)count()函數(shù)
  • count()函數(shù)統(tǒng)計(jì)數(shù)據(jù)表中包含的記錄行的總數(shù),或者根據(jù)查詢結(jié)果返回列中包含的數(shù)據(jù)行數(shù),有兩種使用方法:
    ①count(*)計(jì)算表中總的行數(shù),不管某列有數(shù)值或者為空值。
    ②count(字段名)計(jì)算指定列下總的行數(shù),計(jì)算時(shí)將忽略空值的行。

【例1】查詢customers表中總的行數(shù),SQL語句如下:

mysql> select count(*) as cust_num    -> from customers;+----------+| cust_num |+----------+|        4 |+----------+1 row in set (0.06 sec)

由查詢結(jié)果可知,count(*)返回customers表中記錄的總行數(shù),不管其值是什么。返回的總數(shù)的名稱為cust_num。

【例2】查詢customers表中有電子郵箱的顧客的總數(shù),SQL語句如下:

mysql> select count(c_email) as email_num    -> from customers;+-----------+| email_num |+-----------+|         3 |+-----------+1 row in set (0.00 sec)

上面兩個(gè)例子結(jié)果不同說明兩種方式在計(jì)算總數(shù)的時(shí)候?qū)Υ?a href="http://www.babyishan.com/tag/null">NULL值的方式不同,即指定列的值為空的行被count()函數(shù)忽略,但是如果不指定列,而在count()函數(shù)中使用*,則所有記錄都不忽略。

【例3】在orderitems表中,使用count()函數(shù)與group by關(guān)鍵字一起使用,用來計(jì)算不同分組中的記錄總數(shù)。

mysql> select o_num,count(f_id)     -> from orderitems    -> group by o_num;+-------+-------------+| o_num | count(f_id) |+-------+-------------+| 30001 |           4 || 30002 |           1 || 30003 |           1 || 30004 |           1 || 30005 |           4 |+-------+-------------+5 rows in set (0.00 sec)
(2)sum()函數(shù)
  • sum是一個(gè)求總和的函數(shù),返回指定列值的總和。

【例】在orderitems表中查詢30005號(hào)訂單一共購(gòu)買的水果總量,SQL語句如下:

mysql> select sum(quantity) as items_total    -> from orderitems    -> where o_num = 30005;+-------------+| items_total |+-------------+|          30 |+-------------+1 row in set (0.05 sec)

sum()可以和group by一起使用,用來計(jì)算每個(gè)分組的總和。

【例】在orderitems表中,使用sum()函數(shù)統(tǒng)計(jì)不同訂單號(hào)中訂購(gòu)的水果總量,SQL語句如下:

mysql> select o_num,sum(quantity) as items_total    -> from orderitems    -> group by o_num;+-------+-------------+| o_num | items_total |+-------+-------------+| 30001 |          33 || 30002 |           2 || 30003 |         100 || 30004 |          50 || 30005 |          30 |+-------+-------------+5 rows in set (0.00 sec)

sum函數(shù)在計(jì)算時(shí),忽略值為null的行。

(3)avg()函數(shù)
  • avg()函數(shù)通過計(jì)算返回的行數(shù)和每一列數(shù)據(jù)的和,求得指定列數(shù)據(jù)的平均值。
  • avg()函數(shù)使用時(shí),其參數(shù)為要計(jì)算的列名稱,如果要得到多個(gè)列的多個(gè)平均值,則需要在每一列上使用avg()函數(shù)。

【例1】在fruits表中,查詢s_id=103的供應(yīng)商的水果價(jià)格的平均值,SQL語句如下:

mysql> select avg(f_price) as avg_price    -> from fruits    -> where s_id = 103;+-----------+| avg_price |+-----------+|  5.700000 |+-----------+1 row in set (0.05 sec)

【例2】在fruits表中,查詢每一個(gè)供應(yīng)商的水果價(jià)格的平均值,SQL語句如下:

mysql> select s_id,avg(f_price) as avg_price    -> from fruits    -> group by s_id;+------+-----------+| s_id | avg_price |+------+-----------+|  104 |  7.000000 ||  101 |  6.200000 ||  103 |  5.700000 ||  107 |  3.600000 ||  102 |  8.933333 ||  105 |  7.466667 ||  106 | 15.700000 |+------+-----------+7 rows in set (0.00 sec)

group by 關(guān)鍵字根據(jù)s_id字段對(duì)記錄進(jìn)行分組,然后計(jì)算出每個(gè)分組的平均值,這種分組求平均值的方法非常有用。例如,求不同班級(jí)學(xué)生成績(jī)的平均值,求不同部門工人的平均工資,求各地的年平均氣溫等。

(4)max()函數(shù)
  • max()返回指定列中的最大值。
  • max()函數(shù)除了用來找出最大的列值或日期值之外,還能返回任意列中的最大值,包括返回字符類型的最大值。

【例1】在fruits表中查找市場(chǎng)上價(jià)格最高的水果值,SQL語句如下:

mysql> select max(f_price) as max_price from fruits;+-----------+| max_price |+-----------+|     15.70 |+-----------+1 row in set (0.05 sec)

【例2】在fruits表中查找不同供應(yīng)商提供的價(jià)格最高的水果值,SQL語句如下:

mysql> select s_id,max(f_price) as max_price    -> from fruits    -> group by s_id;+------+-----------+| s_id | max_price |+------+-----------+|  104 |      7.60 ||  101 |     10.20 ||  103 |      9.20 ||  107 |      3.60 ||  102 |     11.20 ||  105 |     11.60 ||  106 |     15.70 |+------+-----------+7 rows in set (0.00 sec)

【例3】在fruits表中查找f_name的最大值,SQL語句如下:

mysql> select max(f_name) from fruits;+-------------+| max(f_name) |+-------------+| xxxx        |+-------------+1 row in set (0.00 sec)
(5)min()函數(shù)

– min()返回查詢列中的最小值。

【例1】在fruits表中查找市場(chǎng)上價(jià)格最低的水果值,SQL語句如下:

mysql> select min(f_price) as min_price    -> from fruits;+-----------+| min_price |+-----------+|      2.20 |+-----------+1 row in set (0.00 sec)

【例2】在fruits表中查找不同供應(yīng)商提供的價(jià)格最低的水果汁,SQL語句如下:

mysql> select s_id,min(f_price) as min_price    -> from fruits    -> group by s_id;+------+-------------+| s_id | min_price |+------+-------------+|  104 |        6.40 ||  101 |        3.20 ||  103 |        2.20 ||  107 |        3.60 ||  102 |        5.30 ||  105 |        2.60 ||  106 |       15.70 |+------+-------------+7 rows in set (0.00 sec)

相關(guān)免費(fèi)學(xué)習(xí)推薦:mysql數(shù)據(jù)庫(視頻)

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊11 分享