MariaDB `Avg()`函數用於檢索表達式的平均值。

``````SELECT AVG(aggregate_expression)
FROM tables
[WHERE conditions];``````

``````SELECT expression1, expression2, ... expression_n,
AVG(aggregate_expression)
FROM tables
[WHERE conditions]
GROUP BY expression1, expression2, ... expression_n;``````

`"testdb"`數據庫中創建一個`"students"`表，並插入一些數據。參考以下創建語句 -

``````USE testdb;
DROP TABLE students;
-- 創建新表
CREATE TABLE students(
student_id INT NOT NULL AUTO_INCREMENT,
student_name VARCHAR(100) NOT NULL,
score float(4, 1) default NULL,
PRIMARY KEY ( student_id )
);
-- 插入數據
INSERT INTO students
VALUES(1,'Maxsu','Haikou', 99.5,'2017-01-07 00:00:00');

INSERT INTO students
VALUES
(2,'Crurry','Beijing',86,'2016-05-07 00:00:00'),
(3,'JMaster','Beijing',91,'2016-05-07 00:00:00'),
(4,'Mahesh','Guangzhou',78,'2016-06-07 00:00:00'),
(5,'Kobe','Shanghai',89,'2016-02-07 00:00:00'),
(6,'Blaba','Shengzhen',100,'2016-08-07 00:00:00');``````

#### 1. AVG()函數與單表達式

``SELECT AVG(Score) AS "Average  Score" FROM students;``

``````MariaDB [testdb]> SELECT AVG(Score) AS "Average  Score" FROM students;
+----------------+
| Average  Score |
+----------------+
|       90.58333 |
+----------------+
1 row in set (0.04 sec)``````

#### 2. AVG()函數與公式

``````MariaDB [testdb]> select * from students;
+------------+--------------+-----------------+----------------+-------+
+------------+--------------+-----------------+----------------+-------+
|          1 | Maxsu        | Haikou          | 2017-01-07     |  99.5 |
|          2 | Crurry       | Beijing         | 2016-05-07     |  86.0 |
|          3 | JMaster      | Beijing         | 2016-05-07     |  91.0 |
|          4 | Mahesh       | Guangzhou       | 2016-06-07     |  78.0 |
|          5 | Kobe         | Shanghai        | 2016-02-07     |  89.0 |
|          6 | Blaba        | Shengzhen       | 2016-08-07     | 100.0 |
+------------+--------------+-----------------+----------------+-------+
6 rows in set (0.07 sec)``````

``SELECT AVG(score * 1.5) AS "New Score"  FROM students;``

``````MariaDB [testdb]> SELECT AVG(score * 1.5) AS "New Score"  FROM students;
+-----------+
| New Score |
+-----------+
| 135.87500 |
+-----------+
1 row in set (0.01 sec)``````

#### 2. AVG()函數與Order By子句

``````INSERT INTO students
VALUES('Maxsu','Haikou', 90,'2017-11-17 00:00:00');``````

``````MariaDB [testdb]> select * from students;
+------------+--------------+-----------------+----------------+-------+
+------------+--------------+-----------------+----------------+-------+
|          1 | Maxsu        | Haikou          | 2017-01-07     |  99.5 |
|          2 | Crurry       | Beijing         | 2016-05-07     |  86.0 |
|          3 | JMaster      | Beijing         | 2016-05-07     |  91.0 |
|          4 | Mahesh       | Guangzhou       | 2016-06-07     |  78.0 |
|          5 | Kobe         | Shanghai        | 2016-02-07     |  89.0 |
|          6 | Blaba        | Shengzhen       | 2016-08-07     | 100.0 |
|          7 | Maxsu        | Haikou          | 2017-11-17     |  90.0 |
+------------+--------------+-----------------+----------------+-------+
7 rows in set (0.00 sec)``````

``````SELECT student_name, AVG(score) AS "Average Salary"
FROM students
GROUP BY student_name;``````

``````MariaDB [testdb]> SELECT student_name, AVG(score) AS "Average Salary"
-> FROM students
-> GROUP BY student_name;
+--------------+----------------+
| student_name | Average Salary |
+--------------+----------------+
| Blaba        |      100.00000 |
| Crurry       |       86.00000 |
| JMaster      |       91.00000 |
| Kobe         |       89.00000 |
| Mahesh       |       78.00000 |
| Maxsu        |       94.75000 |
+--------------+----------------+
6 rows in set (0.02 sec)``````
0 條評論，你可以發表評論，我們會進行改進