日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

学习mysql_day3_高级查询1(聚合查询,聚合统计)

發布時間:2023/12/14 数据库 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 学习mysql_day3_高级查询1(聚合查询,聚合统计) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

聚合函數

先準備數據內容
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 -- 簡單公式

總結

以上是生活随笔為你收集整理的学习mysql_day3_高级查询1(聚合查询,聚合统计)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。