鍍金池/ 教程/ 數(shù)據(jù)庫(kù)/ MariaDB Avg()函數(shù)
MariaDB比較運(yùn)算符
MariaDB查詢數(shù)據(jù)
MariaDB Like子句
MariaDB Sum()函數(shù)
MariaDB過(guò)程
MariaDB限制返回記錄
MariaDB Count()函數(shù)
MariaDB更新數(shù)據(jù)
MariaDB導(dǎo)出數(shù)據(jù)
MariaDB Intersect運(yùn)算符
MariaDB函數(shù)
MariaDB Min()函數(shù)
MariaDB創(chuàng)建數(shù)據(jù)庫(kù)
MariaDB Avg()函數(shù)
MariaDB刪除數(shù)據(jù)
MariaDB條件
MariaDB功能特點(diǎn)
MariaDB創(chuàng)建表
MariaDB左外連接
MariaDB Union運(yùn)算符
MariaDB安裝
MariaDB選擇數(shù)據(jù)庫(kù)
MariaDB Max()函數(shù)
MariaDB Where子句
MariaDB右外連接
MariaDB內(nèi)連接
MariaDB截?cái)啾?/span>
MariaDB Order By子句
MariaDB教程
MariaDB正則表達(dá)式
MariaDB Union All運(yùn)算符
MariaDB Distinct子句
MariaDB修改表
MariaDB刪除表
MariaDB插入數(shù)據(jù)
MariaDB From子句
MariaDB刪除數(shù)據(jù)庫(kù)
MariaDB簡(jiǎn)介
MariaDB數(shù)據(jù)類型

MariaDB Avg()函數(shù)

MariaDB Avg()函數(shù)用于檢索表達(dá)式的平均值。

語(yǔ)法:

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;

數(shù)據(jù)準(zhǔn)備

"testdb"數(shù)據(jù)庫(kù)中創(chuàng)建一個(gè)"students"表,并插入一些數(shù)據(jù)。參考以下創(chuàng)建語(yǔ)句 -

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

INSERT INTO students  
(student_id, student_name, student_address, score, admission_date)  
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()函數(shù)與單表達(dá)式

示例:

查詢Student表的平均分?jǐn)?shù)。參考以下查詢語(yǔ)句 -

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

執(zhí)行上面查詢語(yǔ)句,得到以下結(jié)果 -

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

2. AVG()函數(shù)與公式

也可以在AVG()函數(shù)使用數(shù)學(xué)公式。 例如,如果要將學(xué)生的平均成績(jī)提高50%,則可以使用以下公式來(lái)計(jì)算:

當(dāng)前數(shù)據(jù)庫(kù)中的記錄如下 -

MariaDB [testdb]> select * from students;
+------------+--------------+-----------------+----------------+-------+
| student_id | student_name | student_address | admission_date | score |
+------------+--------------+-----------------+----------------+-------+
|          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;

執(zhí)行上面查詢語(yǔ)句,得到以下結(jié)果 -

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()函數(shù)與Order By子句

為了更容易說(shuō)明問(wèn)題,這里再插入一條記錄 -

INSERT INTO students  
(student_name, student_address,score, admission_date)  
VALUES('Maxsu','Haikou', 90,'2017-11-17 00:00:00');

當(dāng)前students表中,有以下數(shù)據(jù) -

MariaDB [testdb]> select * from students;
+------------+--------------+-----------------+----------------+-------+
| student_id | student_name | student_address | admission_date | score |
+------------+--------------+-----------------+----------------+-------+
|          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)

參考下查詢語(yǔ)句 -

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

執(zhí)行上面查詢語(yǔ)句,得到以下結(jié)果 -

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)