学习mysql_day3_高级查询1(聚合查询,聚合统计)
聚合函數(shù)
先準(zhǔn)備數(shù)據(jù)內(nèi)容
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 | 張學(xué)友 | 36 | 180.00 | 男 | 1 | | | 8 | 周杰倫 | 25 | 166.00 | 男 | 1 | | | 9 | 風(fēng)姐 | 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 | 古天樂(lè) | 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)統(tǒng)計(jì)行數(shù) 有一行算一行
count() --該方法效率高一些
count(1) – 該方法是統(tǒng)計(jì)第一列 如ID
count(列名) --該方法是判斷列有多少行 如果該行有null 則少1 所以查詢(xún)出來(lái)要比對(duì) 性能低
格式為:select count() from 表名; 后面也可以加條件 where 條件
最大值: 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位小數(shù), 四舍五入
計(jì)算所有人的平均年齡,保留2位小數(shù)
MariaDB [mysql_demo1]> select round(avg(height),2) from students; +----------------------+ | round(avg(height),2) | +----------------------+ | 169.94 | +----------------------+ 1 row in set (0.00 sec)聚合統(tǒng)計(jì)
group by
查詢(xún)班級(jí)的學(xué)生性別
有兩種方法 1:distinck 2:group by
查詢(xún)每種性別的人數(shù) 可以結(jié)合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)查詢(xún)每種分組數(shù)據(jù)中的人的姓名 group_concat(列名)
MariaDB [mysql_demo1]> select gender,group_concat(name) from students group by gender; +--------+------------------------------------------------------------------------+ | gender | group_concat(name) | +--------+------------------------------------------------------------------------+ | 男 | 小明,周杰倫,張學(xué)友,劉德化,彭玉宴,周杰,小張,王小明 | | 女 | 金星,張小華,風(fēng)姐,小紅,小月月 | | 中性 | 黃蓉 | | 保密 | 古天樂(lè),劉小海 | +--------+------------------------------------------------------------------------+ 4 rows in set (0.00 sec)查詢(xún)同種性別中的姓名和身高
MariaDB [mysql_demo1]> select gender,group_concat(name,height) from students group by gender; +--------+------------------------------------------------------------------------------------------------------------------------+ | gender | group_concat(name,height) | +--------+------------------------------------------------------------------------------------------------------------------------+ | 男 | 小明180.00,周杰倫166.00,張學(xué)友180.00,劉德化150.00,彭玉宴150.00,周杰175.00,小張160.00,王小明162.00 | | 女 | 金星175.00,張小華173.00,風(fēng)姐169.00,小紅170.00,小月月163.00 | | 中性 | 黃蓉185.00 | | 保密 | 古天樂(lè)186.00,劉小海175.00 | +--------+------------------------------------------------------------------------------------------------------------------------+ 4 rows in set (0.00 sec)對(duì)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,張學(xué)友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,風(fēng)姐169.00,小紅170.00,小月月163.00 | | 中性 | 黃蓉185.00 | | 保密 | 古天樂(lè)186.00,劉小海175.00 | +--------+------------------------------------------------------------------------+ 3 rows in set (0.00 sec)having 和 where 的區(qū)別
having 只能對(duì)分組之后的數(shù)據(jù)做條件篩選, 有having 就一定有 group by, 有 group by 不一定有having
where 是對(duì)查詢(xún)的源數(shù)據(jù)做條件篩選
分頁(yè) 按需加載 limit (start,count)
– limit start, count limit 限制的條數(shù)
– start: 表示從哪里開(kāi)始查詢(xún), start 默認(rèn)值為0, 可以省略, 跳過(guò)多少條數(shù)據(jù)
– count: 查詢(xún)多少條
總結(jié)
以上是生活随笔為你收集整理的学习mysql_day3_高级查询1(聚合查询,聚合统计)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: excel表格末尾添加一行_#天职经验谈
- 下一篇: JavaScript中方法或者变量名称前