让我们首先创建一个表-
mysql> create table DemoTable -> ( -> StudentName varchar(20), -> StudentMarks int -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('Chris',50); mysql> insert into DemoTable values('David',70); mysql> insert into DemoTable values('Chris',80); mysql> insert into DemoTable values('David',90);
使用select语句显示表中的所有记录-
mysql> select * from DemoTable;
这将产生以下输出-
+-------------+--------------+ | StudentName | StudentMarks | +-------------+--------------+ | Chris | 50 | | David | 70 | | Chris | 80 | | David | 90 | +-------------+--------------+ 4 rows in set (0.00 sec)
这是对相应重复记录进行求和的查询,即学生的分数-
mysql> select StudentName,sum(StudentMarks) from DemoTable group by StudentName;
这将产生以下输出-
+-------------+-------------------+ | StudentName | sum(StudentMarks) | +-------------+-------------------+ | Chris | 130 | | David | 160 | +-------------+-------------------+ 2 rows in set (0.00 sec)