詳解MySQL聚合函數(shù)
聚合函數(shù)
在 MySQL 中,聚合函數(shù)是用于計(jì)算多行數(shù)據(jù)的統(tǒng)計(jì)信息的函數(shù),例如總和、平均值、最大值、最小值和行數(shù)等。聚合函數(shù)用于在查詢結(jié)果中創(chuàng)建單個(gè)值,該值代表聚合操作的結(jié)果。將多行數(shù)據(jù)聚合成單個(gè)結(jié)果,這是聚合函數(shù)得名的由來。
以下是 MySQL 中常見的聚合函數(shù):
函數(shù) | 說明 |
---|---|
COUNT([DISTINCT] expr) | 返回查詢到的數(shù)據(jù)的數(shù)量 |
SUM([DISTINCT] expr) | 返回查詢到的數(shù)據(jù)的總和,不是數(shù)字沒有意義 |
AVG([DISTINCT] expr) | 返回查詢到的數(shù)據(jù)的平均值,不是數(shù)字沒有意義 |
MAX([DISTINCT] expr) | 返回查詢到的數(shù)據(jù)的最大值,不是數(shù)字沒有意義 |
MIN([DISTINCT] expr) | 返回查詢到的數(shù)據(jù)的最小值,不是數(shù)字沒有意義 |
這些函數(shù)通常用于 SELECT
查詢語句中,與 GROUP BY
子句結(jié)合使用以對數(shù)據(jù)進(jìn)行分組和匯總。
COUNT 函數(shù)
在 MySQL 中,count
函數(shù)用于計(jì)算指定列或表中行的數(shù)量。
語法:
SELECT COUNT(column_name) FROM table_name;
計(jì)算結(jié)果會忽略指定列中的NULL。
如果要計(jì)算表中所有行的數(shù)量,可以使用以下語法:
SELECT COUNT(*) FROM table_name;
理解:
SELECT COUNT(column_name) FROM table_name; 就是 SELECT column_name FROM table_name; 的結(jié)果的非空行數(shù)
例:
有如下表格
MariaDB [test_db]> select * from student_scores; +----+---------+---------+------+---------+ | id | name | chinese | math | english | +----+---------+---------+------+---------+ | 1 | Alice | 80 | 85 | 90 | | 3 | Charlie | 90 | 95 | 85 | | 4 | Dave | 80 | 90 | 95 | | 5 | Emma | 95 | 85 | 90 | | 6 | Frank | 70 | 78 | 80 | | 7 | God | NULL | NULL | NULL | +----+---------+---------+------+---------+ 6 rows in set (0.00 sec)
查詢總?cè)藬?shù):
MariaDB [test_db]> select count(name) as 總?cè)藬?shù) from student_scores; +-----------+ | 總?cè)藬?shù) | +-----------+ | 6 | +-----------+ 1 row in set (0.00 sec)
實(shí)際上,count()
內(nèi)寫成 *
也可以,甚至寫成 1
這樣的字面值也可以得到正確結(jié)果。
MariaDB [test_db]> select count(*) as 總?cè)藬?shù) from student_scores; +-----------+ | 總?cè)藬?shù) | +-----------+ | 6 | +-----------+ 1 row in set (0.00 sec) MariaDB [test_db]> select count(1) as 總?cè)藬?shù) from student_scores; +-----------+ | 總?cè)藬?shù) | +-----------+ | 6 | +-----------+ 1 row in set (0.00 sec)
這是因?yàn)?* 和 1 都可以作為一個(gè)列,select count(*) as 總?cè)藬?shù) from student_scores;
的結(jié)果就是 select * as 總?cè)藬?shù) from student_scores;
的結(jié)果的行數(shù)。select count(1) as 總?cè)藬?shù) from student_scores;
的結(jié)果是 select 1 as 總?cè)藬?shù) from student_scores;
的結(jié)果的行數(shù)。
統(tǒng)計(jì) chinese
列,NULL
行被忽略
MariaDB [test_db]> select count(chinese) from student_scores; +----------------+ | count(chinese) | +----------------+ | 5 | +----------------+ 1 row in set (0.00 sec)
將 distinct 寫在 count() 內(nèi)外的區(qū)別:
MariaDB [test_db]> select count(distinct chinese) from student_scores; +-------------------------+ | count(distinct chinese) | +-------------------------+ | 4 | +-------------------------+ 1 row in set (0.00 sec) MariaDB [test_db]> select distinct count(chinese) from student_scores; +----------------+ | count(chinese) | +----------------+ | 5 | +----------------+ 1 row in set (0.00 sec)
很明顯,寫在里面才是對去重后的結(jié)果統(tǒng)計(jì)行數(shù),寫在外面是在已經(jīng)統(tǒng)計(jì)好行數(shù)后對count的結(jié)果去重。
SUM 函數(shù)
在 MySQL 中,SUM
是一個(gè)聚合函數(shù),用于計(jì)算指定列或表中所有行的數(shù)值之和??梢詫?nbsp;SUM
用于任何數(shù)值類型的列,包括整數(shù)、小數(shù)等。
語法:
SELECT SUM(column_name) FROM table_name WHERE conditions;
column_name
是要計(jì)算總和的列的名稱
例:
統(tǒng)計(jì)所有人的語文成績的和
MariaDB [test_db]> select sum(chinese) from student_scores; +--------------+ | sum(chinese) | +--------------+ | 415 | +--------------+ 1 row in set (0.00 sec)
AVG 函數(shù)
在 MySQL 中,AVG
是一個(gè)聚合函數(shù),用于計(jì)算指定列或表中所有行的數(shù)值平均值。AVG
函數(shù)僅適用于數(shù)值類型的列,例如整數(shù)或小數(shù)。
語法:
SELECT AVG(column_name) FROM table_name WHERE conditions;
例:
求英語的平均分
MariaDB [test_db]> select avg(english) from student_scores; +--------------+ | avg(english) | +--------------+ | 88.0000 | +--------------+ 1 row in set (0.00 sec)
MAX 函數(shù) MIN 函數(shù)
語法:
SELECT MAX(column_name) FROM table_name WHERE conditions; SELECT MIN(column_name) FROM table_name WHERE conditions;
例:
查詢數(shù)學(xué)是最高分和最低分
MariaDB [test_db]> select max(math) from student_scores; +-----------+ | max(math) | +-----------+ | 95 | +-----------+ 1 row in set (0.00 sec) MariaDB [test_db]> select min(math) from student_scores; +-----------+ | min(math) | +-----------+ | 78 | +-----------+ 1 row in set (0.00 sec)
group by 子句
簡介
上面我們使用聚合函數(shù)后的結(jié)果都只有一行,這是因?yàn)槲覀儼颜麄€(gè)表看成了一個(gè)整體,把一列中的所有行直接聚合成了一個(gè)數(shù)字。
GROUP BY
是用于對結(jié)果集進(jìn)行分組的子句。使用 GROUP BY
可以根據(jù)一個(gè)或多個(gè)列對結(jié)果集進(jìn)行分組,以便在結(jié)果中顯示每個(gè)組的匯總信息。
以下是 GROUP BY
子句的基本語法:
SELECT column_name, aggregate_function(column_name) FROM table_name WHERE conditions GROUP BY column_name;
column_name
是要分組的列的名稱
aggregate_function
是要應(yīng)用于分組的列的聚合函數(shù),例如 SUM
、AVG
、COUNT
等
table_name
是要從中選擇數(shù)據(jù)的表的名稱
conditions
是一個(gè)可選的 WHERE
子句,用于指定選擇數(shù)據(jù)的條件。
示例:scott 數(shù)據(jù)庫
接下來的示例我們使用 scott
數(shù)據(jù)庫,scott
是由 Oracle 公司創(chuàng)建的一個(gè)示例數(shù)據(jù)庫,用于教學(xué)和測試。
scott
數(shù)據(jù)庫的 sql
文件
DROP database IF EXISTS `scott`; CREATE database IF NOT EXISTS `scott` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; USE `scott`; DROP TABLE IF EXISTS `dept`; CREATE TABLE `dept` ( `deptno` int(2) unsigned zerofill NOT NULL COMMENT '部門編號', `dname` varchar(14) DEFAULT NULL COMMENT '部門名稱', `loc` varchar(13) DEFAULT NULL COMMENT '部門所在地點(diǎn)' ); DROP TABLE IF EXISTS `emp`; CREATE TABLE `emp` ( `empno` int(6) unsigned zerofill NOT NULL COMMENT '雇員編號', `ename` varchar(10) DEFAULT NULL COMMENT '雇員姓名', `job` varchar(9) DEFAULT NULL COMMENT '雇員職位', `mgr` int(4) unsigned zerofill DEFAULT NULL COMMENT '雇員領(lǐng)導(dǎo)編號', `hiredate` datetime DEFAULT NULL COMMENT '雇傭時(shí)間', `sal` decimal(7,2) DEFAULT NULL COMMENT '工資月薪', `comm` decimal(7,2) DEFAULT NULL COMMENT '獎(jiǎng)金', `deptno` int(2) unsigned zerofill DEFAULT NULL COMMENT '部門編號' ); DROP TABLE IF EXISTS `salgrade`; CREATE TABLE `salgrade` ( `grade` int(11) DEFAULT NULL COMMENT '等級', `losal` int(11) DEFAULT NULL COMMENT '此等級最低工資', `hisal` int(11) DEFAULT NULL COMMENT '此等級最高工資' ); insert into dept (deptno, dname, loc) values (10, 'ACCOUNTING', 'NEW YORK'); insert into dept (deptno, dname, loc) values (20, 'RESEARCH', 'DALLAS'); insert into dept (deptno, dname, loc) values (30, 'SALES', 'CHICAGO'); insert into dept (deptno, dname, loc) values (40, 'OPERATIONS', 'BOSTON'); insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values (7369, 'SMITH', 'CLERK', 7902, '1980-12-17', 800, null, 20); insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values (7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20', 1600, 300, 30); insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values (7521, 'WARD', 'SALESMAN', 7698, '1981-02-22', 1250, 500, 30); insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values (7566, 'JONES', 'MANAGER', 7839, '1981-04-02', 2975, null, 20); insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values (7654, 'MARTIN', 'SALESMAN', 7698, '1981-09-28', 1250, 1400, 30); insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values (7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01', 2850, null, 30); insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values (7782, 'CLARK', 'MANAGER', 7839, '1981-06-09', 2450, null, 10); insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values (7788, 'SCOTT', 'ANALYST', 7566, '1987-04-19', 3000, null, 20); insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values (7839, 'KING', 'PRESIDENT', null, '1981-11-17', 5000, null, 10); insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values (7844, 'TURNER', 'SALESMAN', 7698,'1981-09-08', 1500, 0, 30); insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values (7876, 'ADAMS', 'CLERK', 7788, '1987-05-23', 1100, null, 20); insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values (7900, 'JAMES', 'CLERK', 7698, '1981-12-03', 950, null, 30); insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values (7902, 'FORD', 'ANALYST', 7566, '1981-12-03', 3000, null, 20); insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values (7934, 'MILLER', 'CLERK', 7782, '1982-01-23', 1300, null, 10); insert into salgrade (grade, losal, hisal) values (1, 700, 1200); insert into salgrade (grade, losal, hisal) values (2, 1201, 1400); insert into salgrade (grade, losal, hisal) values (3, 1401, 2000); insert into salgrade (grade, losal, hisal) values (4, 2001, 3000); insert into salgrade (grade, losal, hisal) values (5, 3001, 9999);
例:
單列分組
查詢每個(gè)部門的平均工資和最高工資
從 emp 表中找,然后對 deptno 分組,分別求平均工資和最高工資
select deptno 部門編號, avg(sal) 平均工資, max(sal) 最高工資 from emp group by deptno;
+--------------+--------------+--------------+ | 部門編號 | 平均工資 | 最高工資 | +--------------+--------------+--------------+ | 10 | 2916.666667 | 5000.00 | | 20 | 2175.000000 | 3000.00 | | 30 | 1566.666667 | 2850.00 | +--------------+--------------+--------------+ 3 rows in set (0.00 sec)
上述示例,group by
會先將表按部門分組,然后對分出的每個(gè)組,分別執(zhí)行 select
語句。
多列分組
查詢每個(gè)部門的每種崗位的平均工資和最低工資
select deptno, job, avg(sal) 平均工資, min(sal) 最低工資 from emp group by deptno, job;
+--------+-----------+--------------+--------------+ | deptno | job | 平均工資 | 最低工資 | +--------+-----------+--------------+--------------+ | 10 | CLERK | 1300.000000 | 1300.00 | | 10 | MANAGER | 2450.000000 | 2450.00 | | 10 | PRESIDENT | 5000.000000 | 5000.00 | | 20 | ANALYST | 3000.000000 | 3000.00 | | 20 | CLERK | 950.000000 | 800.00 | | 20 | MANAGER | 2975.000000 | 2975.00 | | 30 | CLERK | 950.000000 | 950.00 | | 30 | MANAGER | 2850.000000 | 2850.00 | | 30 | SALESMAN | 1400.000000 | 1250.00 | +--------+-----------+--------------+--------------+ 9 rows in set (0.00 sec)
上述用例先按部門分組,然后對每組再按崗位分組,對每個(gè)小組執(zhí)行 select
語句。
下圖展示分組的過程:
having 子句
查詢平均工資低于 2000 的部門及其平均工資
錯(cuò)誤寫法:
select deptno, avg(sal) from emp where avg(sal) < 2000 group by deptno;
where
的執(zhí)行在 group
之前,執(zhí)行 where
的時(shí)候還沒分組吶,根本無法求平均值和篩選。
我們知道,having
篩選在 group by
之后,正確的應(yīng)該用 having
select deptno, avg(sal) from emp group by deptno having avg(sal) < 2000;
總結(jié)
group by
是通過分組,為聚合統(tǒng)計(jì)提供基本的功能支持,即,group by
一定是配合聚合函數(shù)使用的group by
后面跟的是分組的字段依據(jù),只有在group by
后面出現(xiàn)的字段,才能在 select 中作為字段出現(xiàn)having
通常是在完成分組聚合統(tǒng)計(jì),然后再進(jìn)行篩選。where
通常是對表中數(shù)據(jù)進(jìn)行初步篩選,where
后面不能跟聚合函數(shù)。
以上就是詳解MySQL聚合函數(shù)的詳細(xì)內(nèi)容,更多關(guān)于MySQL聚合函數(shù)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
分析Mysql表讀寫、索引等操作的sql語句效率優(yōu)化問題
今天小編就為大家分享一篇關(guān)于分析Mysql表讀寫、索引等操作的sql語句效率優(yōu)化問題,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12在windows上安裝不同(兩個(gè))版本的Mysql數(shù)據(jù)庫的教程詳解
這篇文章主要介紹了在windows上安裝不同(兩個(gè))版本的Mysql數(shù)據(jù)庫 ,需要的朋友可以參考下2019-04-04mysql中Table is read only的解決方法小結(jié)
本文章總結(jié)了關(guān)于在linux與windows中 mysql出現(xiàn)Table is read only解決辦法總結(jié),有需要的朋友可參考一下2013-01-01MySQL/Postgrsql 詳細(xì)講解如何用ODBC接口訪問MySQL指南
2008-01-01