聚合函數
先準備數據內容
MariaDB [mysql_demo1]> select * from students;
+----+-----------+------+--------+--------+--------+-----------+
| id | name | age | height | gender | cls_id | is_delete |
+----+-----------+------+--------+--------+--------+-----------+
| 1 | 小明 | 18 | 180.00 | 男 | 1 | |
| 2 | 小張 | 18 | 160.00 | 男 | 2 | |
| 3 | 小紅 | 20 | 170.00 | 女 | 1 | |
| 4 | 周杰 | 38 | 175.00 | 男 | 1 | |
| 5 | 彭玉宴 | 56 | 150.00 | 男 | 2 | |
| 6 | 劉德化 | 29 | 150.00 | 男 | 2 | |
| 7 | 張學友 | 36 | 180.00 | 男 | 1 | |
| 8 | 周杰倫 | 25 | 166.00 | 男 | 1 | |
| 9 | 風姐 | 16 | 169.00 | 女 | 1 | |
| 10 | 王小明 | 57 | 162.00 | 男 | 2 | |
| 11 | 張小華 | 46 | 173.00 | 女 | 1 | |
| 12 | 金星 | 29 | 175.00 | 女 | 1 | |
| 13 | 黃蓉 | 66 | 185.00 | 中性 | 2 | |
| 14 | 古天樂 | 15 | 186.00 | 保密 | 2 | |
| 15 | 劉小海 | 31 | 175.00 | 保密 | 1 | |
| 16 | 小月月 | 57 | 163.00 | 女 | 1 | |
+----+-----------+------+--------+--------+--------+-----------+
16 rows in set (0.00 sec)
MariaDB [mysql_demo1]> select * from classes;
+----+--------------+
| id | name |
+----+--------------+
| 1 | python_01期 |
| 2 | python_02期 |
+----+--------------+
2 rows in set (0.00 sec)
統計行數 有一行算一行
count() --該方法效率高一些
count(1) – 該方法是統計第一列 如ID
count(列名) --該方法是判斷列有多少行 如果該行有null 則少1 所以查詢出來要比對 性能低
格式爲:select count() from 表名; 後面也可以加條件 where 條件
MariaDB [mysql_demo1]> select count(*) from students;
+----------+
| count(*) |
+----------+
| 16 |
+----------+
1 row in set (0.00 sec)
MariaDB [mysql_demo1]> select count(*) from students where gender='男';
+----------+
| count(*) |
+----------+
| 8 |
+----------+
1 row in set (0.00 sec)
最大值: max()
select max(列名) from 表名 或加條件
MariaDB [mysql_demo1]> select max(height) from students;
+-------------+
| max(height) |
+-------------+
| 186.00 |
+-------------+
1 row in set (0.00 sec)
最小值: min()
select min(列名) from 表名 或加條件
MariaDB [mysql_demo1]> select min(height) from students;
+-------------+
| min(height) |
+-------------+
| 150.00 |
+-------------+
1 row in set (0.00 sec)
求和: sum()
select sum(列名) from 表名 或加條件
MariaDB [mysql_demo1]> select sum(height) from students;
+-------------+
| sum(height) |
+-------------+
| 2719.00 |
+-------------+
1 row in set (0.00 sec)
平均值: avg()
select avg(列名) from 表名 或加條件
MariaDB [mysql_demo1]> select avg(height) from students;
+-------------+
| avg(height) |
+-------------+
| 169.937500 |
+-------------+
1 row in set (0.00 sec)
四捨五入 round(123.23 , 1) 保留1位小數, 四捨五入
計算所有人的平均年齡,保留2位小數
MariaDB [mysql_demo1]> select round(avg(height),2) from students;
+----------------------+
| round(avg(height),2) |
+----------------------+
| 169.94 |
+----------------------+
1 row in set (0.00 sec)
聚合統計
group by
查詢班級的學生性別
有兩種方法 1:distinck 2:group by
MariaDB [mysql_demo1]> select distinct gender from students;
+--------+
| gender |
+--------+
| 男 |
| 女 |
| 中性 |
| 保密 |
+--------+
4 rows in set (0.00 sec)
MariaDB [mysql_demo1]> select gender from students group by gender;
+--------+
| gender |
+--------+
| 男 |
| 女 |
| 中性 |
| 保密 |
+--------+
4 rows in set (0.00 sec)
查詢每種性別的人數 可以結合count(*)
MariaDB [mysql_demo1]> select count(*),gender from students group by gender;
+----------+--------+
| count(*) | gender |
+----------+--------+
| 8 | 男 |
| 5 | 女 |
| 1 | 中性 |
| 2 | 保密 |
+----------+--------+
4 rows in set (0.00 sec)
查詢每種分組數據中的人的姓名 group_concat(列名)
MariaDB [mysql_demo1]> select gender,group_concat(name) from students group by gender;
+--------+------------------------------------------------------------------------+
| gender | group_concat(name) |
+--------+------------------------------------------------------------------------+
| 男 | 小明,周杰倫,張學友,劉德化,彭玉宴,周杰,小張,王小明 |
| 女 | 金星,張小華,風姐,小紅,小月月 |
| 中性 | 黃蓉 |
| 保密 | 古天樂,劉小海 |
+--------+------------------------------------------------------------------------+
4 rows in set (0.00 sec)
查詢同種性別中的姓名和身高
MariaDB [mysql_demo1]> select gender,group_concat(name,height) from students group by gender;
+--------+------------------------------------------------------------------------------------------------------------------------+
| gender | group_concat(name,height) |
+--------+------------------------------------------------------------------------------------------------------------------------+
| 男 | 小明180.00,周杰倫166.00,張學友180.00,劉德化150.00,彭玉宴150.00,周杰175.00,小張160.00,王小明162.00 |
| 女 | 金星175.00,張小華173.00,風姐169.00,小紅170.00,小月月163.00 |
| 中性 | 黃蓉185.00 |
| 保密 | 古天樂186.00,劉小海175.00 |
+--------+------------------------------------------------------------------------------------------------------------------------+
4 rows in set (0.00 sec)
對group by 分組之後做篩選 having 條件
MariaDB [mysql_demo1]> select gender from students group by gender having gender='男';
+--------+
| gender |
+--------+
| 男 |
+--------+
1 row in set (0.00 sec)
MariaDB [mysql_demo1]> select gender,group_concat(name,height) from students group by gender having gender='男';
+--------+------------------------------------------------------------------------------------------------------------------------+
| gender | group_concat(name,height) |
+--------+------------------------------------------------------------------------------------------------------------------------+
| 男 | 小明180.00,周杰倫166.00,張學友180.00,劉德化150.00,彭玉宴150.00,周杰175.00,小張160.00,王小明162.00 |
+--------+------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
having 非等於
MariaDB [mysql_demo1]> select gender from students group by gender having gender!='男';
+--------+
| gender |
+--------+
| 女 |
| 中性 |
| 保密 |
+--------+
3 rows in set (0.00 sec)
MariaDB [mysql_demo1]> select gender,group_concat(name,height) from students group by gender having gender!='男';
+--------+------------------------------------------------------------------------+
| gender | group_concat(name,height) |
+--------+------------------------------------------------------------------------+
| 女 | 金星175.00,張小華173.00,風姐169.00,小紅170.00,小月月163.00 |
| 中性 | 黃蓉185.00 |
| 保密 | 古天樂186.00,劉小海175.00 |
+--------+------------------------------------------------------------------------+
3 rows in set (0.00 sec)
having 和 where 的區別
having 只能對分組之後的數據做條件篩選, 有having 就一定有 group by, 有 group by 不一定有having
where 是對查詢的源數據做條件篩選
分頁 按需加載 limit (start,count)
– limit start, count limit 限制的條數
– start: 表示從哪裏開始查詢, start 默認值爲0, 可以省略, 跳過多少條數據
– count: 查詢多少條
--獲取第一頁, 每頁顯示4條數據
select * from students limit 0,1;
select * from students limit 1;
第1頁 --- 分了幾頁 --- 分了一頁 list (下標) 14 個數
select * from students limit 0,14; 0 是否和id有關聯 1 2
第2頁 --- 從第4條數據 查詢, 查詢出來4條數據
select * from students limit 4,4;
第3頁
select * from students limit (3-1)*4,4;
第4頁
select * from students limit 12,4;
#
select * from students limit (n-1)*m,m;
#每頁顯示m條數據 ,顯示n頁
#100 5 20
# 5
(n-1)*m,m -- 簡單公式