MySQL查询以查找具有相同ID的行的平均值
让我们首先创建一个表-
mysql> create table DemoTable(
StudentId int,
StudentMarks int
);
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values(1000,78);mysql> insert into DemoTable values(1001,88);
mysql> insert into DemoTable values(1000,89);
mysql> insert into DemoTable values(1000,67);
mysql> insert into DemoTable values(1000,90);
mysql> insert into DemoTable values(1001,91);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+-----------+--------------+| StudentId | StudentMarks |
+-----------+--------------+
| 1000 | 78 |
| 1001 | 88 |
| 1000 | 89 |
| 1000 | 67 |
| 1000 | 90 |
| 1001 | 91 |
+-----------+--------------+
6 rows in set (0.00 sec)
以下是查询以查找具有相同ID的行的平均值-
mysql> select StudentId,avg(StudentMarks) from DemoTable group by StudentId;
这将产生以下输出-
+-----------+-------------------+| StudentId | avg(StudentMarks) |
+-----------+-------------------+
| 1000 | 81.0000 |
| 1001 | 89.5000 |
+-----------+-------------------+
2 rows in set (0.03 sec)
以上是 MySQL查询以查找具有相同ID的行的平均值 的全部内容, 来源链接: utcz.com/z/343350.html