在 MySQL 中提取具有 marks1 和 marks2 记录的学生的最大个人分数?


为此,请将 MAX() 与 GROUP BY 子句配合使用。我们首先创建一个表 −

mysql> create table DemoTable
   -> (
   -> StudentEmailId varchar(20),
   -> Marks1 int,
   -> Marks2 int
-> );
Query OK, 0 rows affected (0.90 sec)

使用 insert 命令在表中插入一些记录 −

mysql> insert into DemoTable values('John@gmail.com',45,32);
Query OK, 1 row affected (0.30 sec)
mysql> insert into DemoTable values('John@gmail.com',32,45);
Query OK, 1 row affected (0.34 sec)
mysql> insert into DemoTable values('Carol@gmail.com',32,45);
Query OK, 1 row affected (1.64 sec)
mysql> insert into DemoTable values('David@gmail.com',45,32);
Query OK, 1 row affected (0.18 sec)

使用 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)

这是查询具有 marks1 和 marks2 记录的学生的最大个人分数 −

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)

更新日期: 18-Dec-2019

227 人浏览

开启您的 职业

完成课程即可获得认证

开始
广告
© . All rights reserved.