亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

MySQL 聚合查詢 和 分組查詢示例詳解

 更新時間:2025年04月30日 11:16:38   作者:喜歡做夢  
聚合查詢是SQL中對數(shù)據(jù)進(jìn)行分組統(tǒng)計的操作,可以將多行數(shù)據(jù)按照特定條件合并計算,返回匯總結(jié)果,本文通過實例代碼講解MySQL聚合查詢和 分組查詢,感興趣的朋友一起看看吧

?? 一、聚合查詢

??1.概念

聚合查詢:是SQL中對數(shù)據(jù)進(jìn)行分組統(tǒng)計的操作,可以將多行數(shù)據(jù)按照特定條件合并計算,返回匯總結(jié)果。

??2.聚合查詢函數(shù)

函數(shù)說明
COUNT()統(tǒng)計行數(shù)
SUM()統(tǒng)計數(shù)值列總和
AVG()統(tǒng)計數(shù)值列平均和
MAX()尋找最大值
MIN()尋找最小值
  • 除了函數(shù)COUNT(),其他如果不是數(shù)字沒有意義;
  • 除了函數(shù)COUNT(),可以進(jìn)行全列COUNT(*)查詢,其他不可以;
  • null不參與該查詢;
  • 多個聚合函數(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)
-- 可以指定列查詢,因為chinese中有null,這不會被統(tǒng)計在內(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)計在內(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()求和時,null不參與運算
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ù)里面,一般用于計算某一列中數(shù)值的總和,也就是SUM(列名);
  • 參數(shù)可以使用表達(dá)式;
  • null不參與SUM()運算。 

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

其注意事項與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)
-- 可以同時查詢
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é)果按照一個或多個列的值進(jìn)行分組,把具有相同列值的行歸為一組。找同一組內(nèi)的數(shù)據(jù)可以使用聚合函數(shù)(如COUNT、SUM、MAX、MIN)。

語法

select column1,sum(conumn2),... from table group by column1,colum3;
  • column1:分組的列名;
  • sum(column2): 沒有被分組的列(需要的運算的列),如果要顯示結(jié)果,需要用到聚合函數(shù);
  • group by:分組查詢的關(guān)鍵字;
  • column1:要分組的列名。

示例:

統(tǒng)計每個班級的學(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)
-- 分組:查看每個班級有多少學(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ū)別wherehaving
作用對象分組前對原始數(shù)據(jù)進(jìn)行篩選對分組后的結(jié)果進(jìn)行篩選
使用限制不能使用聚合函數(shù)可以使用聚合函數(shù)

?? 三、插入查詢結(jié)果

插入查詢結(jié)果:把一個表中的數(shù)據(jù)插入到另一個表中 

??語法

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é)生姓名插入到另一個表中 

-- 查看舊表學(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使用TEXT/BLOB類型的知識點詳解

    MySQL使用TEXT/BLOB類型的知識點詳解

    在本篇文章里小編給大家整理的是關(guān)于MySQL使用TEXT/BLOB類型的幾點注意內(nèi)容,有興趣的朋友們學(xué)習(xí)下。
    2020-03-03
  • win10 下安裝mysql服務(wù)器社區(qū)版本mysql 5.7.22 winx64的圖文教程

    win10 下安裝mysql服務(wù)器社區(qū)版本mysql 5.7.22 winx64的圖文教程

    這篇文章主要介紹了win10 下安裝mysql服務(wù)器社區(qū)版本mysql 5.7.22 winx64的圖文教程,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-05-05
  • MySQL修改時區(qū)的方法圖文詳解

    MySQL修改時區(qū)的方法圖文詳解

    這篇文章主要給大家介紹了關(guān)于MySQL修改時區(qū)的相關(guān)資料,時區(qū)是MySQL中非常重要的概念,正確設(shè)置時區(qū)可以確保應(yīng)用程序工作正常并避免時間錯誤,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-09-09
  • 新手學(xué)習(xí)MySQL索引

    新手學(xué)習(xí)MySQL索引

    如果正確合理設(shè)計并且使用索引的MySQL是一輛蘭博基尼的話,那么沒有索引的MySQL就是一個人力三輪車。通常大型網(wǎng)站單日就可能會產(chǎn)生幾十萬甚至幾百萬的數(shù)據(jù),沒有索引查詢會變的非常緩慢,下面小編來帶大家了解下索引
    2019-05-05
  • 淺談Mysql使用B+樹來實現(xiàn)索引的原因

    淺談Mysql使用B+樹來實現(xiàn)索引的原因

    這篇文章,主要來探討一下為什么Mysql使用B+樹來實現(xiàn)索引,這里討論的目標(biāo)是Mysql的InnoDB存儲引擎.可以想象一下,如果你是Mysql的開發(fā)人員,你會怎么去選擇合適的數(shù)據(jù)結(jié)構(gòu)呢,感興趣的小伙伴跟著小編一起來探討吧
    2023-05-05
  • RPM方式安裝MySQL5.6源碼

    RPM方式安裝MySQL5.6源碼

    這篇文章主要為大家分享了RPM方式安裝MySQL5.6源碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • MySQL建表設(shè)置默認(rèn)值/取值范圍的操作代碼

    MySQL建表設(shè)置默認(rèn)值/取值范圍的操作代碼

    這篇文章主要介紹了MySQL建表設(shè)置默認(rèn)值/取值范圍的操作代碼,文中給大家提到了MySQL創(chuàng)建表時字符串的默認(rèn)值,本文給大家講解的非常詳細(xì),需要的朋友可以參考下
    2022-11-11
  • MySQL中無過濾條件的count詳解

    MySQL中無過濾條件的count詳解

    這篇文章主要給大家介紹了關(guān)于MySQL中無過濾條件count的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧
    2019-02-02
  • MySQL做讀寫分離提高性能緩解數(shù)據(jù)庫壓力

    MySQL做讀寫分離提高性能緩解數(shù)據(jù)庫壓力

    這篇文章主要為大家介紹了MySQL做讀寫分離提高性能緩解數(shù)據(jù)庫壓力的技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • mysql合并字符串的實現(xiàn)

    mysql合并字符串的實現(xiàn)

    這篇文章主要介紹了mysql合并字符串的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08

最新評論