为此,您可以使用子查询。让我们首先创建一个表-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)
以下是仅查找前三个列值的平均值的查询。在这里,我们使用LIMIT 3来查找三个记录的平均值-mysql> select avg(Score)
from ( select Score from DemoTable limit 3) tbl;
这将产生以下输出-+------------+
| avg(Score) |
+------------+
| 60.0000 |
+------------+
1 row in set (0.00 sec)