MySQL查询从具有五个值的列中查找前三个值的平均值
为此,您可以使用子查询。让我们首先创建一个表-
mysql> create table DemoTable ( Score int );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values(80); mysql> insert into DemoTable values(45); mysql> insert into DemoTable values(55); mysql> insert into DemoTable values(78); mysql> insert into DemoTable values(88);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+-------+ | Score | +-------+ | 80 | | 45 | | 55 | | 78 | | 88 | +-------+ 5 rows in set (0.00 sec)
以下是仅查找前三个列值的平均值的查询。在这里,我们使用LIMIT3来查找三个记录的平均值-
mysql> select avg(Score) from ( select Score from DemoTable limit 3) tbl;
这将产生以下输出-
+------------+ | avg(Score) | +------------+ | 60.0000 | +------------+ 1 row in set (0.00 sec)
热门推荐
10 祝孙辈生日祝福语简短
11 恋人跨年祝福语大全简短
12 考大学敬酒简短祝福语
13 开工投产大吉祝福语 简短
14 偶然结婚祝福语朋友简短
15 感谢学长学姐祝福语简短
16 戏精的祝福语简短
17 我要结婚祝福语简短
18 婚前女儿生日祝福语简短