如何从包含学生成绩的表中获取第二高的值?

mysqlmysqli database更新于 2025/12/10 7:08:17

要获取第二高的值,请使用 ORDER BY DESC 和 LIMIT 1,1。让我们首先创建一个表 −

mysql> create table DemoTable
(
   StudentScore int
);
Query OK, 0 rows affected (0.56 sec)

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

mysql> insert into DemoTable values(89);
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable values(69);
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values(97);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values(99);
Query OK, 1 row affected (0.12 sec)

使用 select 语句显示表中的所有记录 −

mysql> select *from DemoTable;

这将产生以下输出 −

+--------------+
| StudentScore |
+--------------+
|           89 |
|           69 |
|           97 |
|           99 |
+--------------+
4 rows in set (0.00 sec)

以下是获取第二高值的查询 −

mysql> select *from DemoTable order by StudentScore DESC limit 1,1;

这将产生以下输出 −

+--------------+
| StudentScore |
+--------------+
|           97 |
+--------------+
1 row in set (0.00 sec)

相关文章


有用资源