要选择MySQL中的第n个最大值,请使用以下语法-
select distinct(yourColumnName) from yourTableName order by yourColumnName DESC limit (NthValue-1),1;
让我们首先创建一个表-
mysql> create table DemoTable1594 -> ( -> Marks int -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1594 values(76); mysql> insert into DemoTable1594 values(95); mysql> insert into DemoTable1594 values(56); mysql> insert into DemoTable1594 values(96); mysql> insert into DemoTable1594 values(89);
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1594;
这将产生以下输出-
+-------+ | Marks | +-------+ | 76 | | 95 | | 56 | | 96 | | 89 | +-------+ 5 rows in set (0.00 sec)
这是选择第n个最大值的查询-
mysql> select distinct(Marks) from DemoTable1594 order by Marks DESC limit 2,1;
这将产生以下输出-
+-------+ | Marks | +-------+ | 89 | +-------+ 1 row in set (0.00 sec)