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

歡迎訪問 生活随笔!

生活随笔

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

数据库

mysql function 表名作为参数_mysql 常用的分组聚合函数

發布時間:2023/12/2 数据库 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql function 表名作为参数_mysql 常用的分组聚合函数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

mysql 常用的分組聚合函數

1.聚合運算

一般情況下,需要的聚合數據(和,平均值,最大,最小等)并不總是存儲在表中,但是可以執行存儲數據的計算來獲取它.

根據定義,聚合函數對一組值執行計算并返回單個值.

MySQL提供了許多聚合函數,包括AVG,COUNT,SUM,MIN,MAX等.除COUNT函數外,其它聚合函數在執行計算時會忽略NULL值.

有如下prices 表,分別進行聚合操作

?use test;-- 創建表 create table prices( id int not null, count1 int, count2 int, count3 int, primary key (id)) engine=InnoDB default charset utf8mb4;insert into prices values(1,20,null,null),(2,50,500,null),(3,390,262,null),(4,28,234,null);select * from prices;idcount1count2count3
120nullnull
250500null
3390262null
428234null

注意:

  • 函數不允許嵌套使用,比如:count(max(..))

  • 函數的參數可以是列或者函數表達式

  • 一個select語句可以出現多個聚合函數

avg():求平均值

  • 自動忽略null 值

?mysql root@192.168.101:test> select avg(count1),avg(count2),avg(count3) from pri ? ? ? ? ? ? ? ? ? ? ? ? ? ? ces; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? +-------------+-------------+-------------+| avg(count1) | avg(count2) | avg(count3) |+-------------+-------------+-------------+| 122.0000 ? | 332.0000 ? | <null> ? ? |+-------------+-------------+-------------+

count() :統計出現的滿足條件的次數.

  • 不忽略null 值

?-- count 計數-- 返回count2中等于500的個數 select count(count2) from prices where count2=500;-- 返回總共的行數select count(*) from prices;-- 返回不重復的值select count(distinct count2) from prices;

max()\min()\sum()

  • 自動忽略null 值

?select max(count1),min(count2),sum(count3) from prices;

2.分組計算

基本語法:

?select 聚合函數(字段名) ?from 表名where 查詢條件group by 字段名having 過濾條件

1.gruop by

作用于聚合函數,根據給定的列或表達式的每一個不同的值將表中的行分成不同的組,使用函數返回每一組的統計信息.

?SELECT column_name, aggregate_function(column_name) -- aggregate_function 聚合函數FROM table_nameWHERE column_name operator valueGROUP BY column_name;
  • 出現在select子句中的單獨的列,必須出現在group by 子句中作為分組列

  • 分組列可以不出現在select 子句中

  • 分組列可出現在select 子句中的一個復合表達式中

  • 如果group by 后面是一個復合表達式,那么在select子句中,它必須整體作為表達式的一部分才能使用

有如下員工表(db_employee):

?-- 創建員工表create table db_employee( id int(11) not null auto_increment, ? name varchar(20) not null, ? ?date datetime not null, ? singin tinyint(4) not null default 0 comment '登陸后臺次數', ? ?primary key (id)) engine=InnoDB default charset utf8;insert into db_employee values(1, 'Jack', '2016-04-19 15:26:02', 1),(2, 'Peny', '2016-04-11 15:26:02', 4),(3, 'Jony', '2016-04-12 15:26:02', 2),(4, 'Bob', '2016-04-13 15:26:02', 4),(5, 'Harry', '2016-04-15 15:26:02', 6),(6, 'Peny', '2016-04-17 15:26:02', 4);select * from db_employee;idnamedatesingin
1Jack2016-04-19 15:26:021
2Peny2016-04-11 15:26:024
3Jony2016-04-12 15:26:022
4Bob2016-04-13 15:26:024
5Harry2016-04-15 15:26:026
6Peny2016-04-17 15:26:024

將數據表按名字進行分組,并統計每個人有多少條記錄

?mysql root@192.168.101:test> select name,count(*) as number ?from db_employee group by name; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? +-------+--------+| name | number |+-------+--------+| Bob ? | 1 ? ? || Harry | 1 ? ? || Jack | 1 ? ? || Jony | 1 ? ? || Peny | 2 ? ? |+-------+--------+

將數據表按名字進行分組,并統計每個人登錄多少次(分組求和)

?mysql root@192.168.101:test> select name,sum(singin) from db_employee group by name; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? +-------+-------------+| name | sum(singin) |+-------+-------------+| Bob ? | 4 ? ? ? ? ? || Harry | 6 ? ? ? ? ? || Jack | 1 ? ? ? ? ? || Jony | 2 ? ? ? ? ? || Peny | 8 ? ? ? ? ? |+-------+-------------+

更改數據,并先按照名字分組,在按照登陸次數分組(多組分)

?update db_employee set singin=5 where id = 6;select name,singin from db_employee group by name,singin;

gruop by: 首先將select語句得到一個結果集,然后按照分組字段,將具有相同分組字段的記錄歸類成一條記錄.

2.having

語法:

?SELECT column_name, aggregate_function(column_name)FROM table_nameWHERE column_name operator valueGROUP BY column_nameHAVING aggregate_function(column_name) operator value;

where 子句比group by 先執行,而函數必須在gruop by 之后再執行,那么在group by 之后可以使用having 子句進行結果集的過濾

  • where 子句在分組前對記錄進行過濾

  • having 子句在分組后對記錄進行過濾

having 的用法

  • having 可以單獨使用不和group by 配合,如果只有having ,表中所有的列分為一組

  • having 子句中可以使用 函數

  • having 子句中的列,那么出現在函數中,那么出現在group by 子句中,否則報錯.

?-- 顯示id大于2,并且按照人名進行排列,排列的結果要求大于4select name, sum(singin) as count from db_employee where id > 2 group by name having sum(singin) > 4;

總結

以上是生活随笔為你收集整理的mysql function 表名作为参数_mysql 常用的分组聚合函数的全部內容,希望文章能夠幫你解決所遇到的問題。

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