1. 程式人生 > >MySQL之分組函式

MySQL之分組函式

分組函式

功能:用作統計使用,又稱為聚合函式或統計函式或組函式。

分類:
sum 求和、avg 平均值、max 最大值 、min 最小值 、count 計算個數。

特點:

  • 1、sum、avg一般用於處理數值型

             max、min、count可以處理任何型別

  • 2、以上分組函式都忽略null值
  • 3、可以和distinct搭配實現去重的運算
  • 4、count函式的單獨介紹

             一般使用count(*)用作統計行數

  • 5、和分組函式一同查詢的欄位要求是group by後的欄位

1、求和函式

SELECT SUM(salary) 
FROM employees;

輸出結果:

mysql> SELECT SUM(salary)
    -> FROM employees;
+-------------+
| SUM(salary) |
+-------------+
|   691400.00 |
+-------------+
1 row in set (0.00 sec)

2、求平均函式

SELECT AVG(salary)
FROM employees;

輸出結果:

mysql> SELECT SUM(salary)
    -> FROM employees;
+-------------+
| SUM(salary) |
+-------------+
|   691400.00 |
+-------------+
1 row in set (0.00 sec)

3、最小值

SELECT MIN(salary)
FROM employees;

輸出結果:

mysql> SELECT MIN(salary)
    -> FROM employees;
+-------------+
| MIN(salary) |
+-------------+
|     2100.00 |
+-------------+
1 row in set (0.00 sec)

4、統計個數

SELECT COUNT(*)
FROM employees;

輸出結果:

mysql> SELECT COUNT(*)
    -> FROM employees;
+----------+
| COUNT(*) |
+----------+
|      107 |
+----------+
1 row in set (0.00 sec)

5、綜合使用案例

SELECT SUM(salary) 和,AVG(salary) 平均,MAX(salary) 最高,MIN(salary) 最低,COUNT(salary) 個數
FROM employees;

輸出結果:

mysql> SELECT SUM(salary) 和,AVG(salary) 平均,MAX(salary) 最高,MIN(salary) 最低,COUNT(salary) 個數
    -> FROM employees;
+-----------+-------------+----------+---------+------+
| 和        | 平均        | 最高     | 最低    | 個數 |
+-----------+-------------+----------+---------+------+
| 691400.00 | 6461.682243 | 24000.00 | 2100.00 |  107 |
+-----------+-------------+----------+---------+------+
1 row in set (0.00 sec)
SELECT SUM(salary) 和, ROUND(AVG(salary), 2) 平均, MAX(salary) 最高, MIN(salary) 最低, COUNT(salary) 個數
FROM employees;

輸出結果:

mysql> SELECT SUM(salary) 和, ROUND(AVG(salary), 2) 平均,
    -> MAX(salary) 最高, MIN(salary) 最低, COUNT(salary) 個數
    -> FROM employees;
+-----------+---------+----------+---------+------+
| 和        | 平均    | 最高     | 最低    | 個數 |
+-----------+---------+----------+---------+------+
| 691400.00 | 6461.68 | 24000.00 | 2100.00 |  107 |
+-----------+---------+----------+---------+------+
1 row in set (0.00 sec)

2、引數支援哪些型別

1. sum, avg 對於字串型別,不會報錯,但是沒有實際意義

SELECT SUM(last_name) ,AVG(last_name) FROM employees;

輸出結果:

mysql> SELECT SUM(last_name) ,AVG(last_name) FROM employees;
+----------------+----------------+
| SUM(last_name) | AVG(last_name) |
+----------------+----------------+
|              0 |              0 |
+----------------+----------------+
1 row in set, 214 warnings (0.00 sec)

2、sum, avg 日期型別

SELECT SUM(hiredate) ,AVG(hiredate) FROM employees;

輸出結果:

mysql> SELECT SUM(hiredate) ,AVG(hiredate) FROM employees;
+------------------+---------------------+
| SUM(hiredate)    | AVG(hiredate)       |
+------------------+---------------------+
| 2148552443000000 | 20079929373831.7757 |
+------------------+---------------------+
1 row in set (0.00 sec)

3、max, min函式對於字串型別的支援

SELECT MAX(last_name),MIN(last_name) FROM employees;

輸出結果:

mysql> SELECT MAX(last_name),MIN(last_name) FROM employees;
+----------------+----------------+
| MAX(last_name) | MIN(last_name) |
+----------------+----------------+
| Zlotkey        | Abel           |
+----------------+----------------+
1 row in set (0.00 sec)

4、max, min函式對於日期型別的支援

SELECT MAX(hiredate),MIN(hiredate) FROM employees;

輸出結果:

mysql> SELECT MAX(hiredate),MIN(hiredate) FROM employees;
+---------------------+---------------------+
| MAX(hiredate)       | MIN(hiredate)       |
+---------------------+---------------------+
| 2016-03-03 00:00:00 | 1992-04-03 00:00:00 |
+---------------------+---------------------+
1 row in set (0.00 sec)

5、count函式會忽略欄位的null值,進行統計

SELECT COUNT(commission_pct) FROM employees;

輸出結果:

mysql> SELECT COUNT(commission_pct) FROM employees;
+-----------------------+
| COUNT(commission_pct) |
+-----------------------+
|                    35 |
+-----------------------+
1 row in set (0.00 sec)

案例:使用count函式進行名稱統計

SELECT COUNT(last_name) FROM employees;

輸出結果:

mysql> SELECT COUNT(last_name) FROM employees;
+------------------+
| COUNT(last_name) |
+------------------+
|              107 |
+------------------+
1 row in set (0.00 sec)

3、是否忽略null

SELECT SUM(commission_pct) ,AVG(commission_pct),SUM(commission_pct)/35,SUM(commission_pct)/107 FROM employees;

SELECT MAX(commission_pct) ,MIN(commission_pct) FROM employees;

SELECT COUNT(commission_pct) FROM employees;
SELECT commission_pct FROM employees;

會忽略null值。

4、和distinct搭配

SELECT SUM(DISTINCT salary),SUM(salary) FROM employees;

SELECT COUNT(DISTINCT salary),COUNT(salary) FROM employees;

5、count函式的詳細介紹

SELECT COUNT(salary) FROM employees;

SELECT COUNT(*) FROM employees;

SELECT COUNT(1) FROM employees;

效率:
     MYISAM儲存引擎下  ,COUNT(*)的效率高;
     INNODB儲存引擎下,COUNT(*)和COUNT(1)的效率差不多,比COUNT(欄位)要高一些。

6、和分組函式一同查詢的欄位有限制

SELECT AVG(salary), employee_id 
FROM employees;

輸出結果:

mysql> SELECT AVG(salary), employee_id
    -> FROM employees;
+-------------+-------------+
| AVG(salary) | employee_id |
+-------------+-------------+
| 6461.682243 |         100 |
+-------------+-------------+
1 row in set (0.00 sec)