MySQL 聚合查詢 和 分組查詢示例詳解
?? 一、聚合查詢
??1.概念
聚合查詢:是SQL中對數(shù)據(jù)進(jìn)行分組統(tǒng)計(jì)的操作,可以將多行數(shù)據(jù)按照特定條件合并計(jì)算,返回匯總結(jié)果。
??2.聚合查詢函數(shù)
| 函數(shù) | 說明 |
| COUNT() | 統(tǒng)計(jì)行數(shù) |
| SUM() | 統(tǒng)計(jì)數(shù)值列總和 |
| AVG() | 統(tǒng)計(jì)數(shù)值列平均和 |
| MAX() | 尋找最大值 |
| MIN() | 尋找最小值 |
- 除了函數(shù)COUNT(),其他如果不是數(shù)字沒有意義;
- 除了函數(shù)COUNT(),可以進(jìn)行全列COUNT(*)查詢,其他不可以;
- null不參與該查詢;
- 多個(gè)聚合函數(shù)可以同時(shí)使用。
示例:
-- 創(chuàng)建學(xué)生成績表
mysql> create table student_grade(
-> id bigint auto_increment primary key,
-> name varchar(20),
-> chinese bigint,
-> math bigint,
-> english bigint);
Query OK, 0 rows affected (0.07 sec)
mysql> insert into student_grade(name,chinese,math,english) values('張三',89,95,65),
-> ('李四',96,88,67),('王柿子',78,91,75),('張亮',99,73,97);
Query OK, 4 rows affected (0.13 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> insert into student_grade(name,chinese,math,english) values('麗麗',null,56,89);
Query OK, 1 row affected (0.05 sec)
mysql> select* from student_grade;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 1 | 張三 | 89 | 95 | 65 |
| 2 | 李四 | 96 | 88 | 67 |
| 3 | 王柿子 | 78 | 91 | 75 |
| 4 | 張亮 | 99 | 73 | 97 |
| 5 | 麗麗 | NULL | 56 | 89 |
+----+-----------+---------+------+---------+
5 rows in set (0.00 sec)COUNT()
mysql> select* from student_grade; +----+-----------+---------+------+---------+ | id | name | chinese | math | english | +----+-----------+---------+------+---------+ | 1 | 張三 | 89 | 95 | 65 | | 2 | 李四 | 96 | 88 | 67 | | 3 | 王柿子 | 78 | 91 | 75 | | 4 | 張亮 | 99 | 73 | 97 | | 5 | 麗麗 | NULL | 56 | 89 | +----+-----------+---------+------+---------+ 5 rows in set (0.00 sec) -- 推薦使用COUNT(*)查詢 mysql> select count(*) from student_grade; +----------+ | count(*) | +----------+ | 5 | +----------+ 1 row in set (0.00 sec) -- 當(dāng)然,也可以使用常量 mysql> select count(1) from student_grade; +----------+ | count(1) | +----------+ | 5 | +----------+ 1 row in set (0.00 sec) -- 可以指定列查詢,因?yàn)閏hinese中有null,這不會被統(tǒng)計(jì)在內(nèi) mysql> select count(chinese) from student_grade; +----------------+ | count(chinese) | +----------------+ | 4 | +----------------+ 1 row in set (0.00 sec)
- 一般使用COUNT(*)來查詢,里面也可以使用常量,當(dāng)更推薦使用*;
- 也可以指定列查詢;
- 當(dāng)列中包含null,null不會被統(tǒng)計(jì)在內(nèi)。
SUM()
mysql> select* from student_grade; +----+-----------+---------+------+---------+ | id | name | chinese | math | english | +----+-----------+---------+------+---------+ | 1 | 張三 | 89 | 95 | 65 | | 2 | 李四 | 96 | 88 | 67 | | 3 | 王柿子 | 78 | 91 | 75 | | 4 | 張亮 | 99 | 73 | 97 | | 5 | 麗麗 | NULL | 56 | 89 | +----+-----------+---------+------+---------+ 5 rows in set (0.00 sec) -- 查詢數(shù)學(xué)成績總和 mysql> select sum(math) from student_grade; +-----------+ | sum(math) | +-----------+ | 403 | +-----------+ 1 row in set (0.03 sec) -- 參數(shù)可以使用表達(dá)式 mysql> select sum(math+chinese+english) as total from student_grade; +-------+ | total | +-------+ | 1013 | +-------+ 1 row in set (0.04 sec) -- 查詢語文成績總和 -- 之前說到null與任何值結(jié)果相加都為null,chinese有null值但是其結(jié)果并不為null -- 原因:在sum()求和時(shí),null不參與運(yùn)算 mysql> select sum(chinese) from student_grade; +--------------+ | sum(chinese) | +--------------+ | 362 | +--------------+ 1 row in set (0.00 sec) -- *一般用來取所有列,不能直接用在sun()函數(shù)里 mysql> select sum(*) from student_grade; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*) from student_grade' at line 1
- *不能直接使用sum()函數(shù)里面,一般用于計(jì)算某一列中數(shù)值的總和,也就是SUM(列名);
- 參數(shù)可以使用表達(dá)式;
- null不參與SUM()運(yùn)算。
AVG()
-- 查詢語文成績平均值 mysql> select avg(chinese) from student_grade; +--------------+ | avg(chinese) | +--------------+ | 90.5000 | +--------------+ 1 row in set (0.00 sec) -- 查詢數(shù)學(xué)成績平均值 mysql> select avg(math) from student_grade; +-----------+ | avg(math) | +-----------+ | 80.6000 | +-----------+ 1 row in set (0.00 sec) -- 不能使用* mysql> select avg(*) from student_grade; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*) from student_grade' at line 1
其注意事項(xiàng)與SUM()相似
MAX()和MIN()
mysql> select* from student_grade; +----+-----------+---------+------+---------+ | id | name | chinese | math | english | +----+-----------+---------+------+---------+ | 1 | 張三 | 89 | 95 | 65 | | 2 | 李四 | 96 | 88 | 67 | | 3 | 王柿子 | 78 | 91 | 75 | | 4 | 張亮 | 99 | 73 | 97 | | 5 | 麗麗 | NULL | 56 | 89 | +----+-----------+---------+------+---------+ 5 rows in set (0.00 sec) -- 查詢數(shù)學(xué)成績最大值 mysql> select max(math) from student_grade; +-----------+ | max(math) | +-----------+ | 95 | +-----------+ 1 row in set (0.04 sec) -- 查詢語文成績最小值 mysql> select min(chinese) from student_grade; +--------------+ | min(chinese) | +--------------+ | 78 | +--------------+ 1 row in set (0.00 sec) -- 可以同時(shí)查詢 mysql> select max(chinese),min(chinese) from student_grade; +--------------+--------------+ | max(chinese) | min(chinese) | +--------------+--------------+ | 99 | 78 | +--------------+--------------+ 1 row in set (0.00 sec) -- 使用別名 mysql> select max(chinese)數(shù)學(xué)最大值 ,min(chinese)語文最小值 from student_grade; +-----------------+-----------------+ | 數(shù)學(xué)最大值 | 語文最小值 | +-----------------+-----------------+ | 99 | 78 | +-----------------+-----------------+ 1 row in set (0.00 sec)
?? 二、分組查詢
??1.GROUP BY子句 定義
定義:GROUP BY是SQL中用于分組聚合的核心子句,用于將查詢結(jié)果按照一個(gè)或多個(gè)列的值進(jìn)行分組,把具有相同列值的行歸為一組。找同一組內(nèi)的數(shù)據(jù)可以使用聚合函數(shù)(如COUNT、SUM、MAX、MIN)。
語法
select column1,sum(conumn2),... from table group by column1,colum3;
- column1:分組的列名;
- sum(column2): 沒有被分組的列(需要的運(yùn)算的列),如果要顯示結(jié)果,需要用到聚合函數(shù);
- group by:分組查詢的關(guān)鍵字;
- column1:要分組的列名。
示例:
統(tǒng)計(jì)每個(gè)班級的學(xué)生數(shù)量
-- 創(chuàng)建學(xué)生表
mysql> create table students(
-> class_id bigint, -- 學(xué)生所在班級
-> name varchar(20)); -- 學(xué)生姓名
Query OK, 0 rows affected (0.04 sec)
-- 插入
mysql> insert into students values(1,'楊楊'),(3,'麗麗'),(1,'小美'),(2,'小帥'),(3,'王五');
Query OK, 5 rows affected (0.04 sec)
Records: 5 Duplicates: 0 Warnings: 0
-- 查看學(xué)生表
mysql> select* from students;
+----------+--------+
| class_id | name |
+----------+--------+
| 1 | 楊楊 |
| 3 | 麗麗 |
| 1 | 小美 |
| 2 | 小帥 |
| 3 | 王五 |
+----------+--------+
5 rows in set (0.00 sec)
-- 分組:查看每個(gè)班級有多少學(xué)生
mysql> select class_id,count(class_id) as student_count from students group by class_id;
+----------+---------------+
| class_id | student_count |
+----------+---------------+
| 1 | 2 |
| 3 | 2 |
| 2 | 1 |
+----------+---------------+
3 rows in set (0.01 sec)
-- 將其按班級編號進(jìn)行升序排序
-- group by后面可以跟order by
mysql> select class_id,count(class_id) as student_count from students group by class_id order by class_id asc;
+----------+---------------+
| class_id | student_count |
+----------+---------------+
| 1 | 2 |
| 2 | 1 |
| 3 | 2 |
+----------+---------------+
3 rows in set (0.00 sec)??2.HAVING 定義
定義:對分組結(jié)果進(jìn)行過濾,group by子句進(jìn)行分組以后,不能使用where語句,而需要用HAVING。
-- 篩選分組后班級編號小于2的班級 mysql> select class_id,count(class_id) as student_count from students group by class_id having class_id<=2; +----------+---------------+ | class_id | student_count | +----------+---------------+ | 1 | 2 | | 2 | 1 | +----------+---------------+ 2 rows in set (0.00 sec)
having必須和group by一起用,having要跟在group by后面;
having與where的區(qū)別
| 區(qū)別 | where | having |
| 作用對象 | 分組前對原始數(shù)據(jù)進(jìn)行篩選 | 對分組后的結(jié)果進(jìn)行篩選 |
| 使用限制 | 不能使用聚合函數(shù) | 可以使用聚合函數(shù) |
?? 三、插入查詢結(jié)果
插入查詢結(jié)果:把一個(gè)表中的數(shù)據(jù)插入到另一個(gè)表中
??語法
insert into table_name [(column1,column2,...)] select column1,colum2,... from another_table
- able_name : 被插入數(shù)據(jù)的表名;
- another_table : 源表,即數(shù)據(jù)來源的表;
- []:表示可寫可不寫,如果寫了,那么colum1,colum2,...需要加上括號(),并且插入的列數(shù)量和數(shù)據(jù)類型要與被插入的列數(shù)量與數(shù)據(jù)類型相同。
示例:
將舊學(xué)生表1中的學(xué)生姓名插入到另一個(gè)表中
-- 查看舊表學(xué)生表1找你中的信息:
mysql> select * from students;
+----------+--------+
| class_id | name |
+----------+--------+
| 1 | 楊楊 |
| 3 | 麗麗 |
| 1 | 小美 |
| 2 | 小帥 |
| 3 | 王五 |
+----------+--------+
5 rows in set (0.02 sec)
-- 創(chuàng)建新表
mysql> create table new_student(
-> id bigint auto_increment primary key,
-> name varchar(20));
Query OK, 0 rows affected (0.09 sec)
-- 將舊表中的學(xué)生名復(fù)制到新表中
mysql> insert into new_student (name) select name from students;
Query OK, 5 rows affected (0.08 sec)
Records: 5 Duplicates: 0 Warnings: 0
-- 查看新表中的信息
mysql> select * from new_student;
+----+--------+
| id | name |
+----+--------+
| 1 | 楊楊 |
| 2 | 麗麗 |
| 3 | 小美 |
| 4 | 小帥 |
| 5 | 王五 |
+----+--------+
5 rows in set (0.00 sec)到此這篇關(guān)于MySQL 聚合查詢 和 分組查詢的文章就介紹到這了,更多相關(guān)mysql聚合查詢 和 分組查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySQL從MyISAM轉(zhuǎn)換成InnoDB錯誤與常用解決辦法
由于一些程序的要求,需要MyISAM數(shù)據(jù)引擎或InnoDB,下面是具體的解決方法,經(jīng)測試偶爾會出現(xiàn)一些問題。2011-05-05
MySQL中order?by排序時(shí)數(shù)據(jù)存在null則排序在最前面的方法
order by排序是最常用的功能,但是排序有時(shí)會遇到數(shù)據(jù)為空null的情況,這樣排序就會亂了,這篇文章主要給大家介紹了關(guān)于MySQL中order?by排序時(shí)數(shù)據(jù)存在null則排序在最前面的相關(guān)資料,需要的朋友可以參考下2024-06-06
MySQL?RC事務(wù)隔離的實(shí)現(xiàn)原理
這篇文章主要介紹了MySQL?RC事務(wù)隔離的實(shí)現(xiàn),Read?Committed,事務(wù)運(yùn)行期間,只要別的事務(wù)修改數(shù)據(jù)并提交,即可讀到人家修改的數(shù)據(jù),所以會有不可重復(fù)讀、幻讀問題,下文相關(guān)介紹需要的朋友可以參考下2022-03-03
Mysql LONGTEXT 類型存儲大文件(二進(jìn)制也可以) (修改+調(diào)試+整理)
MySql2.cpp : Defines the entry point for the console application.2009-07-07

