?個人主頁:?喜歡做夢
歡迎??👍點贊? ?關注? ??收藏? 💬評論
目錄
🌴 一、聚合查詢
🌲1.概念
🌲2.聚合查詢函數
COUNT()
?SUM()
AVG()
?MAX()和MIN()
🌴?二、分組查詢
🍀1.GROUP BY子句
定義
語法
?🍀2.HAVING
定義
having與where的區別
🌴?三、插入查詢結果
🍃語法
🌴 一、聚合查詢
🌲1.概念
聚合查詢:是SQL中對數據進行分組統計的操作,可以將多行數據按照特定條件合并計算,返回匯總結果。
🌲2.聚合查詢函數
函數 | 說明 |
COUNT() | 統計行數 |
SUM() | 統計數值列總和 |
AVG() | 統計數值列平均和 |
MAX() | 尋找最大值 |
MIN() | 尋找最小值 |
- 除了函數COUNT(),其他如果不是數字沒有意義;
- 除了函數COUNT(),可以進行全列COUNT(*)查詢,其他不可以;
- null不參與該查詢;
- 多個聚合函數可以同時使用。
示例:?
-- 創建學生成績表
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: 0mysql> 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)-- 當然,也可以使用常量
mysql> select count(1) from student_grade;
+----------+
| count(1) |
+----------+
| 5 |
+----------+
1 row in set (0.00 sec)-- 可以指定列查詢,因為chinese中有null,這不會被統計在內
mysql> select count(chinese) from student_grade;
+----------------+
| count(chinese) |
+----------------+
| 4 |
+----------------+
1 row in set (0.00 sec)
- 一般使用COUNT(*)來查詢,里面也可以使用常量,當更推薦使用*;
- 也可以指定列查詢;
- 當列中包含null,null不會被統計在內。?
?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)-- 查詢數學成績總和
mysql> select sum(math) from student_grade;
+-----------+
| sum(math) |
+-----------+
| 403 |
+-----------+
1 row in set (0.03 sec)-- 參數可以使用表達式
mysql> select sum(math+chinese+english) as total from student_grade;
+-------+
| total |
+-------+
| 1013 |
+-------+
1 row in set (0.04 sec)-- 查詢語文成績總和
-- 之前說到null與任何值結果相加都為null,chinese有null值但是其結果并不為null
-- 原因:在sum()求和時,null不參與運算
mysql> select sum(chinese) from student_grade;
+--------------+
| sum(chinese) |
+--------------+
| 362 |
+--------------+
1 row in set (0.00 sec)-- *一般用來取所有列,不能直接用在sun()函數里
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()函數里面,一般用于計算某一列中數值的總和,也就是SUM(列名);
- 參數可以使用表達式;
- null不參與SUM()運算。?
AVG()
-- 查詢語文成績平均值
mysql> select avg(chinese) from student_grade;
+--------------+
| avg(chinese) |
+--------------+
| 90.5000 |
+--------------+
1 row in set (0.00 sec)-- 查詢數學成績平均值
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)-- 查詢數學成績最大值
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)數學最大值 ,min(chinese)語文最小值 from student_grade;
+-----------------+-----------------+
| 數學最大值 | 語文最小值 |
+-----------------+-----------------+
| 99 | 78 |
+-----------------+-----------------+
1 row in set (0.00 sec)
🌴?二、分組查詢
🍀1.GROUP BY子句
定義
?定義:GROUP BY是SQL中用于分組聚合的核心子句,用于將查詢結果按照一個或多個列的值進行分組,把具有相同列值的行歸為一組。找同一組內的數據可以使用聚合函數(如COUNT、SUM、MAX、MIN)。
語法
select column1,sum(conumn2),... from table group by column1,colum3;
- column1:分組的列名;
- sum(column2):?沒有被分組的列(需要的運算的列),如果要顯示結果,需要用到聚合函數;
- group by:分組查詢的關鍵字;
- column1:要分組的列名。
示例:
統計每個班級的學生數量
-- 創建學生表
mysql> create table students(-> class_id bigint, -- 學生所在班級-> name varchar(20)); -- 學生姓名
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-- 查看學生表
mysql> select* from students;
+----------+--------+
| class_id | name |
+----------+--------+
| 1 | 楊楊 |
| 3 | 麗麗 |
| 1 | 小美 |
| 2 | 小帥 |
| 3 | 王五 |
+----------+--------+
5 rows in set (0.00 sec)-- 分組:查看每個班級有多少學生
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)-- 將其按班級編號進行升序排序
-- 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
定義
定義:對分組結果進行過濾,group by子句進行分組以后,不能使用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的區別
區別 | where | having |
作用對象 | 分組前對原始數據進行篩選 | 對分組后的結果進行篩選 |
使用限制 | 不能使用聚合函數 | 可以使用聚合函數 |
🌴?三、插入查詢結果
插入查詢結果:把一個表中的數據插入到另一個表中?
🍃語法
insert into table_name [(column1,column2,...)]
select column1,colum2,...
from another_table
- able_name : 被插入數據的表名;
- another_table : 源表,即數據來源的表;
- []:表示可寫可不寫,如果寫了,那么colum1,colum2,...需要加上括號(),并且插入的列數量和數據類型要與被插入的列數量與數據類型相同。
示例:
將舊學生表1中的學生姓名插入到另一個表中?
-- 查看舊表學生表1找你中的信息:
mysql> select * from students;
+----------+--------+
| class_id | name |
+----------+--------+
| 1 | 楊楊 |
| 3 | 麗麗 |
| 1 | 小美 |
| 2 | 小帥 |
| 3 | 王五 |
+----------+--------+
5 rows in set (0.02 sec)-- 創建新表
mysql> create table new_student(-> id bigint auto_increment primary key,-> name varchar(20));
Query OK, 0 rows affected (0.09 sec)-- 將舊表中的學生名復制到新表中
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)