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)
热门推荐
10 情侣祝福语古文简短独特
11 酒店伴娘祝福语简短
12 晚上甜蜜的祝福语简短
13 给新娘简短的祝福语
14 当爸爸的简短祝福语
15 洛宁新年祝福语大全简短
16 新年祝福语简短搞笑幽默
17 结婚祝福语大全简短霸气
18 新人结婚祝福语简短高端