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;| 1 | 20 | null | null |
| 2 | 50 | 500 | null |
| 3 | 390 | 262 | null |
| 4 | 28 | 234 | null |
注意:
函數不允許嵌套使用,比如:count(max(..))
函數的參數可以是列或者函數表達式
一個select語句可以出現多個聚合函數
avg():求平均值
自動忽略null 值
count() :統計出現的滿足條件的次數.
不忽略null 值
max()\min()\sum()
自動忽略null 值
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;| 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 |
將數據表按名字進行分組,并統計每個人有多少條記錄
?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 子句中,否則報錯.
總結
以上是生活随笔為你收集整理的mysql function 表名作为参数_mysql 常用的分组聚合函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎样用无线上网卡连接连接无线路由器上网卡
- 下一篇: python网站数据写入mysql_py