借助MySQL聚合函数计算列中数字的平均值AVG()
。
语法如下-
select avg(yourColumnName) as anyVariableName from yourTableName;
为了理解上述概念,让我们创建一个表。以下是创建表的查询。
mysql> create table AverageCalculateDemo −> ( −> SubjectMarks int −> );
以下是将一些记录插入表中的查询-
mysql> insert into AverageCalculateDemo values(70); mysql> insert into AverageCalculateDemo values(80); mysql> insert into AverageCalculateDemo values(65); mysql> insert into AverageCalculateDemo values(55); mysql> insert into AverageCalculateDemo values(60);
在选择语句的帮助下显示所有值。查询如下以显示所有记录-
mysql> select *from AverageCalculateDemo;
以下是输出-
+--------------+ | SubjectMarks | +--------------+ | 70 | | 80 | | 65 | | 55 | | 60 | +--------------+ 5 rows in set (0.00 sec)
这是计算MySQL中列平均值的查询-
mysql> select avg(SubjectMarks) as AverageOf4Numbers from AverageCalculateDemo;
以下是显示平均值的输出-
+-------------------+ | AverageOf4Numbers | +-------------------+ | 66.0000 | +-------------------+ 1 row in set (0.00 sec)