最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2025年04月30日 11:16:38   作者:喜歡做夢  
聚合查詢是SQL中對數(shù)據(jù)進(jìn)行分組統(tǒng)計(jì)的操作,可以將多行數(shù)據(jù)按照特定條件合并計(jì)算,返回匯總結(jié)果,本文通過實(shí)例代碼講解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ū)別wherehaving
作用對象分組前對原始數(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)文章

最新評論

桐乡市| 台东县| 亚东县| 富顺县| 池州市| 搜索| 金寨县| 商丘市| 南投县| 黎川县| 子长县| 南靖县| 鲁山县| 尖扎县| 汾阳市| 高阳县| 东台市| 静海县| 乐都县| 宜章县| 阿克陶县| 阳春市| 八宿县| 卫辉市| 娱乐| 景东| 遂川县| 大余县| 耒阳市| 河源市| 苍山县| 牡丹江市| 隆化县| 大悟县| 石泉县| 筠连县| 鄂托克旗| 台东县| 蒙城县| 兴文县| 措美县|