共計(jì) 3865 個字符,預(yù)計(jì)需要花費(fèi) 10 分鐘才能閱讀完成。
自動寫代碼機(jī)器人,免費(fèi)開通
丸趣 TV 小編給大家分享一下 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ì)算時將忽略空值的行。
【例 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)
上面兩個例子結(jié)果不同說明兩種方式在計(jì)算總數(shù)的時候?qū)Υ?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 是一個求總和的函數(shù),返回指定列值的總和。
【例】在 orderitems 表中查詢 30005 號訂單一共購買的水果總量,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ì)算每個分組的總和。
【例】在 orderitems 表中,使用 sum()函數(shù)統(tǒng)計(jì)不同訂單號中訂購的水果總量,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ì)算時,忽略值為 null 的行。
(3)avg()函數(shù)
avg()函數(shù)通過計(jì)算返回的行數(shù)和每一列數(shù)據(jù)的和,求得指定列數(shù)據(jù)的平均值。
avg()函數(shù)使用時,其參數(shù)為要計(jì)算的列名稱,如果要得到多個列的多個平均值,則需要在每一列上使用 avg()函數(shù)。
【例 1】在 fruits 表中,查詢 s_id=103 的供應(yīng)商的水果價格的平均值,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 表中,查詢每一個供應(yīng)商的水果價格的平均值,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 字段對記錄進(jìn)行分組,然后計(jì)算出每個分組的平均值,這種分組求平均值的方法非常有用。例如,求不同班級學(xué)生成績的平均值,求不同部門工人的平均工資,求各地的年平均氣溫等。
(4)max()函數(shù)
max()返回指定列中的最大值。
max()函數(shù)除了用來找出最大的列值或日期值之外,還能返回任意列中的最大值,包括返回字符類型的最大值。
【例 1】在 fruits 表中查找市場上價格最高的水果值,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)商提供的價格最高的水果值,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 表中查找市場上價格最低的水果值,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)商提供的價格最低的水果汁,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)
看完了這篇文章,相信你對“MySQL 數(shù)據(jù)查詢中如何使用集合 / 聚合函數(shù)查詢”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注丸趣 TV 行業(yè)資訊頻道,感謝各位的閱讀!
向 AI 問一下細(xì)節(jié)