在 MySQL 中获取具有 marks1 和 marks2 记录的学生的最高个人分数?
mysqli mysqldatabase更新于 2024/8/30 23:08:17
为此,请使用 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)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

