如何计算MySQL中数据库行之间的平均值?
为此,您可以使用AVG()
。以下是语法-
select avg(yourColumnName1) as anyAliasName1, avg(yourColumnName2) as anyAliasName2, avg(yourColumnName3) as anyAliasName3, . . N from yourTableName;
让我们创建一个表-
mysql> create table demo31 −> ( −> value1 int, −> value2 int, −> value3 int −> );
借助insert命令将一些记录插入表中-
mysql> insert into demo31 values(34,55,67); mysql> insert into demo31 values(50,60,70); mysql> insert into demo31 values(100,200,300); mysql> insert into demo31 values(10,300,200);
使用select语句显示表中的记录-
mysql> select *from demo31;
这将产生以下输出-
+--------+--------+--------+ | value1 | value2 | value3 | +--------+--------+--------+ | 34 | 55 | 67 | | 50 | 60 | 70 | | 100 | 200 | 300 | | 10 | 300 | 200 | +--------+--------+--------+ 4 rows in set (0.00 sec)
以下是查询以计算跨数据库行的平均值-
mysql> select avg(value1) as Value1_AVG, −> avg(value2) as Value2_AVG, −> avg(value3) as Value3_AVG −> from demo31;
这将产生以下输出-
+------------+------------+------------+ | Value1_AVG | Value2_AVG | Value3_AVG | +------------+------------+------------+ | 48.5000 | 153.7500 | 159.2500 | +------------+------------+------------+ 1 row in set (0.00 sec)