每一次做統(tǒng)計,涉及到稍微復(fù)雜點的sql去問老大起暮,老大都會笑著說卖氨,這不都是mysql最基本的東西,你該回去給msyql補(bǔ)課了~~~趁清明節(jié)负懦,搞搞M厕唷!纸厉!
mysql中什么是聚合函數(shù)系吭?其實聚合函數(shù)還有一個定義:組函數(shù)!
在每一行集合上進(jìn)行操作颗品,對每一個組給出一個結(jié)果肯尺!
一沃缘、常用的組函數(shù):
avg:求平均值
count:統(tǒng)計行的數(shù)量
max:求最大值
min:求最小值
sum:求和
注意:
1、每組函數(shù)接收一個參數(shù)
2则吟、默認(rèn)情況下槐臀,組函數(shù)忽略值為null的行,不參與計算
3氓仲、使用關(guān)鍵字distinct剔除字段值重復(fù)的條數(shù)
一水慨、count函數(shù)使用demo數(shù)據(jù)
mysql> select * from salary_tab;
+--------+---------+
| userid | salary |
+--------+---------+
| 1 | 1000.00 |
| 2 | 2000.00 |
| 3 | 3000.00 |
| 4 | NULL |
| 5 | 1000.00 |
+--------+---------+
rows in set (0.00 sec)
mysql> use TENNIS
mysql> show tables;
+-------------------+
| Tables_in_TENNIS |
+-------------------+
| COMMITTEE_MEMBERS |
| MATCHES |
| PENALTIES |
| PLAYERS |
| TEAMS |
+-------------------+
rows in set (0.00 sec)
(1)、count(*):返回表中滿足where條件的行的數(shù)量
mysql> select count(*) from salary_tab where salary='1000';
+----------+
| count(*) |
+----------+
| 2 |
+----------+
mysql> select count(*) from salary_tab; #沒有條件寨昙,默認(rèn)統(tǒng)計表數(shù)據(jù)行數(shù)
+----------+
| count(*) |
+----------+
| 5 |
+----------+
(2)讥巡、count(列):返回列值非空的行的數(shù)量
mysql> select count(salary) from salary_tab;
+---------------+
| count(salary) |
+---------------+
| 4 |
+---------------+
(3)、count(distinct 列):返回列值非空的舔哪、并且列值不重復(fù)的行的數(shù)量
mysql> select count(distinct salary) from salary_tab;
+------------------------+
| count(distinct salary) |
+------------------------+
| 3 |
+------------------------+
(4)欢顷、count(expr):根據(jù)表達(dá)式統(tǒng)計數(shù)據(jù)(to_days返回的是天數(shù),不懂可以查下)
mysql> select * from TT;
+------+------------+
| UNIT | DATE |
+------+------------+
| a | 2018-04-03 |
| a | 2017-12-12 |
| b | 2018-01-01 |
| b | 2018-04-03 |
| c | 2016-06-06 |
| d | 2018-03-03 |
+------+------------+
rows in set (0.00 sec)
mysql> select UNIT as '單位',
-> COUNT(TO_DAYS(DATE)=TO_DAYS(NOW()) or null) as '今日統(tǒng)計',
-> COUNT(YEAR(DATE)=YEAR(NOW()) or null) as '今年統(tǒng)計'
-> from TT
-> group by UNIT;
+------+----------+----------+
| 單位 | 今日統(tǒng)計 | 今年統(tǒng)計 |
+------+----------+----------+
| a | 1 | 1 |
| b | 1 | 2 |
| c | 0 | 0 |
| d | 0 | 1 |
+------+----------+----------+
rows in set (0.00 sec)
二捉蚤、max和min使用demo
mysql> select max(salary) from salary_tab;
+-------------+
| max(salary) |
+-------------+
| 3000.00 |
+-------------+
mysql> select min(salary) from salary_tab;
+-------------+
| min(salary) |
+-------------+
| 1000.00 |
+-------------+
如果統(tǒng)計的列中只有NULL值抬驴,那么MAX和MIN就返回NULL
三、sum和avg函數(shù)---求和與求平均
@虑伞布持!注意:
表中列值為null的行不參與計算
mysql> select sum(salary) from salary_tab;
+-------------+
| sum(salary) |
+-------------+
| 7000.00 |
+-------------+
mysql> select avg(salary) from salary_tab;
+-------------+
| avg(salary) |
+-------------+
| 1750.000000 |
+-------------+
mysql> select avg(ifnull(salary,0)) from salary_tab;
+-----------------------+
| avg(ifnull(salary,0)) |
+-----------------------+
| 1400.000000 |
+-----------------------+
要想列值為NULL的行也參與組函數(shù)的計算,必須使用IFNULL函數(shù)對NULL值做轉(zhuǎn)換
二陕悬、SELECT分組:
1题暖、SELECT分組的基本格式
select [聚合函數(shù)] 字段名 from 表名 [where 查詢條件] [group by 字段名] [having 過濾條件]
(1)、group by子句:
根據(jù)給定列或者表達(dá)式的每一個不同的值將表中的行分成不同的組捉超,使用組函數(shù)返回每一組的統(tǒng)計信息
規(guī)則:
①出現(xiàn)在SELECT子句中的單獨的列胧卤,必須出現(xiàn)在GROUP BY子句中作為分組列
②分組列可以不出現(xiàn)在SELECT子句中
③分組列可出現(xiàn)在SELECT子句中的一個復(fù)合表達(dá)式中
④如果GROUP BY后面是一個復(fù)合表達(dá)式,那么在SELECT子句中拼岳,它必須整體作為一個表達(dá)式的一部分才能使用枝誊。
1)指定一個列進(jìn)行分組
mysql> select salary,count(*) from salary_tab
-> where salary>=2000
-> group by salary;
+---------+----------+
| salary | count(*) |
+---------+----------+
| 2000.00 | 1 |
| 3000.00 | 1 |
+---------+----------+
2)指定多個分組列,‘大組中再分小組’
mysql> select userid,count(salary) from salary_tab
-> where salary>=2000
-> group by salary,userid;
+--------+---------------+
| userid | count(salary) |
+--------+---------------+
| 2 | 1 |
| 3 | 1 |
+--------+---------------+
3)根據(jù)表達(dá)式分組
mysql> select year(payment_date),count(*)
-> from PENALTIES
-> group by year(payment_date);
+--------------------+----------+
| year(payment_date) | count(*) |
+--------------------+----------+
| 1980 | 3 |
| 1981 | 1 |
| 1982 | 1 |
| 1983 | 1 |
| 1984 | 2 |
+--------------------+----------+
rows in set (0.00 sec)
4)帶有排序的分組:如果分組列和排序列相同惜纸,則可以合并group by和order by子句
mysql> select teamno,count(*)
-> from MATCHES
-> group by teamno
-> order by teamno desc;
+--------+----------+
| teamno | count(*) |
+--------+----------+
| 2 | 5 |
| 1 | 8 |
+--------+----------+
rows in set (0.00 sec)
mysql> select teamno,count(*)
-> from MATCHES
-> group by teamno desc; #可以把desc(或者asc)包含到group by子句中簡化
+--------+----------+
| teamno | count(*) |
+--------+----------+
| 2 | 5 |
| 1 | 8 |
+--------+----------+
rows in set (0.00 sec)
2叶撒、GROUP_CONCAT()函數(shù)
函數(shù)的值等于屬于一個組的指定列的所有值,以逗號隔開耐版,并且以字符串表示祠够。
例1:對于每個球隊,得到其編號和所有球員的編號
mysql> select teamno,group_concat(playerno)
-> from MATCHES
-> group by teamno;
+--------+------------------------+
| teamno | group_concat(playerno) |
+--------+------------------------+
| 1 | 6,6,6,44,83,2,57,8 |
| 2 | 27,104,112,112,8 |
+--------+------------------------+
rows in set (0.01 sec)
如果沒有g(shù)roup by子句粪牲,group_concat返回一列的所有值
例2:得到所有的罰款編號列表
mysql> select group_concat(paymentno)
-> from PENALTIES;
+-------------------------+
| group_concat(paymentno) |
+-------------------------+
| 1,2,3,4,5,6,7,8 |
+-------------------------+
row in set (0.00 sec)
4哪审、HAVING子句:對分組結(jié)果進(jìn)行過濾
注意:
不能使用WHERE子句對分組后的結(jié)果進(jìn)行過濾
不能在WHERE子句中使用組函數(shù),僅用于過濾行
因為WHERE子句比GROUP BY先執(zhí)行虑瀑,而組函數(shù)必須在分完組之后才執(zhí)行湿滓,且分完組后必須使用having子句進(jìn)行結(jié)果集的過濾滴须。
SELECT select_expr [, select_expr ...]
FROM table_name
[WHERE where_condition]
[GROUP BY {col_name | expr} [ASC | DESC], ... [WITH ROLLUP]]
[HAVING where_condition]
!_窗隆扔水!having子語句與where子語句區(qū)別:
where子句在分組前對記錄進(jìn)行過濾;
having子句在分組后對記錄進(jìn)行過濾
mysql> select salary,count(*) from salary_tab
-> where salary>=2000
-> group by salary
-> having count(*)>=0;
+---------+----------+
| salary | count(*) |
+---------+----------+
| 2000.00 | 1 |
| 3000.00 | 1 |
+---------+----------+
1)HAVING可以單獨使用而不和GROUP BY配合,如果只有HAVING子句而沒有GROUP BY朝氓,表中所有的行分為一組
2)HAVING子句中可以使用組函數(shù)
3)HAVING子句中的列魔市,要么出現(xiàn)在一個組函數(shù)中,要么出現(xiàn)在GROUP BY子句中(否則出錯)
mysql> select town,count(*)
-> from PLAYERS
-> group by town
-> having birth_date>'1970-01-01';
ERROR 1054 (42S22): Unknown column 'birth_date' in 'having clause'
mysql> select town,count(*)
-> from PLAYERS
-> group by town
-> having town in ('Eltham','Midhurst');
+----------+----------+
| town | count(*) |
+----------+----------+
| Eltham | 2 |
| Midhurst | 1 |
+----------+----------+
rows in set (0.00 sec)
基本就這么多了赵哲、待德、、以后再補(bǔ)充