SQL AVG() 函数

SQL 教程 · 2019-03-04 07:28:51

AVG() 函数

AVG() 函数返回数值列的平均值。

SQL AVG() 语法

SELECT AVG(column_name) FROM table_name

演示数据库

在本教程中,我们将使用 CODERCTO 样本数据库。

下面是选自 "access_log" 表的数据:

+-----+---------+-------+------------+
| aid | site_id | count | date       |
+-----+---------+-------+------------+
|   1 |       1 |    45 | 2016-05-10 |
|   2 |       3 |   100 | 2016-05-13 |
|   3 |       1 |   230 | 2016-05-14 |
|   4 |       2 |    10 | 2016-05-14 |
|   5 |       5 |   205 | 2016-05-14 |
|   6 |       4 |    13 | 2016-05-15 |
|   7 |       3 |   220 | 2016-05-15 |
|   8 |       5 |   545 | 2016-05-16 |
|   9 |       3 |   201 | 2016-05-17 |
+-----+---------+-------+------------+

SQL AVG() 实例

下面的 SQL 语句从 "access_log" 表的 "count" 列获取平均值:

实例

SELECT AVG(count) AS CountAverage FROM access_log;

执行以上 SQL 输出结果如下:

下面的 SQL 语句选择访问量高于平均访问量的 "site_id" 和 "count":

实例

SELECT site_id, count FROM access_log
WHERE count > (SELECT AVG(count) FROM access_log);

执行以上 SQL 输出结果如下:


点击查看所有 SQL 教程 文章: https://codercto.com/courses/l/29.html

查看所有标签

Programming the Mobile Web

Programming the Mobile Web

Maximiliano Firtman / O'Reilly Media / 2010-07-23 / $44.99

* Learn how to use your existing skills to move into mobile web development * Discover the new possibilities of mobile web development, and understand its limitations * Get detailed coverage of ......一起来看看 《Programming the Mobile Web》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

在线进制转换器
在线进制转换器

各进制数互转换器

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具