为此,请MAX()与GROUP BY子句一起使用。让我们首先创建一个表-mysql> create table DemoTable
-> (
-> StudentEmailId varchar(20),
-> Marks1 int,
-> Marks2 int
-> );
使用插入命令在表中插入一些记录-mysql> insert into DemoTable values('John@gmail.com',45,32);
mysql> insert into DemoTable values('John@gmail.com',32,45);
mysql> insert into DemoTable values('Carol@gmail.com',32,45);
mysql> insert into DemoTable values('David@gmail.com',45,32);
使用select语句显示表中的所有记录-mysql> select *from DemoTable ;
这将产生以下输出-+-----------------+--------+--------+
| StudentEmailId | Marks1 | Marks2 |
+-----------------+--------+--------+
| John@gmail.com | 45 | 32 |
| John@gmail.com | 32 | 45 |
| Carol@gmail.com | 32 | 45 |
| David@gmail.com | 45 | 32 |
+-----------------+--------+--------+
4 rows in set (0.00 sec)
这是查询以获取具有标记1和标记2记录的学生的最大单个标记的查询-mysql> select StudentEmailId,max(Marks1),max(Marks2) from DemoTable
-> group by StudentEmailId;
这将产生以下输出-+-----------------+-------------+-------------+
| StudentEmailId | max(Marks1) | max(Marks2) |
+-----------------+-------------+-------------+
| John@gmail.com | 45 | 45 |
| Carol@gmail.com | 32 | 45 |
| David@gmail.com | 45 | 32 |
+-----------------+-------------+-------------+
3 rows in set (0.00 sec)