SQL AVG() 函数

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

SELECT AVG(column_name) FROM table_name;

演示数据

先在 MySQL 数据库运行下面的语句创建测试数据

CREATE DATABASE IF NOT EXISTS twle default character set utf8mb4 collate utf8mb4_unicode_ci;

USE twle;

CREATE TABLE IF NOT EXISTS `lession_views` (
    uniq bigint(20) primary key NOT NULL default '0' ,
    lession_name varchar(32) NOT NULL default '',
    lession_id int(11) NOT NULL default '0',
    date_at  int(11) NOT NULL default '0',
    views int(11) NOT NULL default '0'
);

INSERT INTO lession_views(uniq,lession_name,lession_id,date_at,views) VALUES
(20170511000001,'Python 基础教程',1,20170511,320),
(20170511000002,'Scala 基础教程', 2,20170511,22),
(20170511000003,'Ruby 基础教程', 3, 20170511,49),
(20170512000001,'Python 基础教程',1,20170512,220),
(20170512000002,'Scala 基础教程',2,20170512,12),
(20170512000003,'Ruby 基础教程',3,20170512,63),
(20170513000001,'Python 基础教程',1,20170513,441),
(20170513000002,'Scala 基础教程',2,20170513,39),
(20170513000003,'Ruby 基础教程',3,20170513,87);

使用 SELECT * FROM lession_views; 运行结果如下

+----------------+---------------------+------------+----------+-------+
| uniq           | lession_name        | lession_id | date_at  | views |
+----------------+---------------------+------------+----------+-------+
| 20170511000001 | Python 基础教程     |          1 | 20170511 |   320 |
| 20170511000002 | Scala 基础教程      |          2 | 20170511 |    22 |
| 20170511000003 | Ruby 基础教程       |          3 | 20170511 |    49 |
| 20170512000001 | Python 基础教程     |          1 | 20170512 |   220 |
| 20170512000002 | Scala 基础教程      |          2 | 20170512 |    12 |
| 20170512000003 | Ruby 基础教程       |          3 | 20170512 |    63 |
| 20170513000001 | Python 基础教程     |          1 | 20170513 |   441 |
| 20170513000002 | Scala 基础教程      |          2 | 20170513 |    39 |
| 20170513000003 | Ruby 基础教程       |          3 | 20170513 |    87 |
+----------------+---------------------+------------+----------+-------+

范例

  1. 如果我们想要知道总的平均访问数,可以使用下面的 SQL 语句

    SELECT AVG(views) FROM lession_views;
    

    运行结果如下

    +------------+
    | AVG(views) |
    +------------+
    |   139.2222 |
    +------------+
    
  2. 如果想要知道每门课程的平均访问数,则需要用到 GROUP BY 语句

    SELECT lession_name, AVG(views) FROM lession_views GROUP BY lession_name;
    

    运行结果如下

    +---------------------+------------+
    | lession_name        | AVG(views) |
    +---------------------+------------+
    | Python 基础教程     |   327.0000 |
    | Ruby 基础教程       |    66.3333 |
    | Scala 基础教程      |    24.3333 |
    +---------------------+------------+
    
  3. 如果想要知道平均每天的访问数,则可以使用下面的 SQL 语句

    SELECT date_at, AVG(views) FROM lession_views GROUP BY date_at;
    

    运行结果如下

    +----------+------------+
    | date_at  | AVG(views) |
    +----------+------------+
    | 20170511 |   130.3333 |
    | 20170512 |    98.3333 |
    | 20170513 |   189.0000 |
    

SQL 基础教程

关于   |   FAQ   |   我们的愿景   |   广告投放   |  博客

  简单教程,简单编程 - IT 入门首选站

Copyright © 2013-2018 简单教程 twle.cn All Rights Reserved.