是的,我们可以从表中选择第二大记录,而无需使用LIMIT子句。我们首先来看一个示例并创建一个表-
mysql> create table DemoTable ( Number int );
使用插入命令在表中插入记录-
mysql> insert into DemoTable values(78); mysql> insert into DemoTable values(67); mysql> insert into DemoTable values(92); mysql> insert into DemoTable values(98); mysql> insert into DemoTable values(88); mysql> insert into DemoTable values(86); mysql> insert into DemoTable values(89);
以下是使用select语句显示表中所有记录的查询-
mysql> select *from DemoTable;
这将产生以下输出-
+--------+ | Number | +--------+ | 78 | | 67 | | 92 | | 98 | | 88 | | 86 | | 89 | +--------+ 7 rows in set (0.00 sec)
以下是从表中选择第二大数字而不使用LIMIT子句的查询-
mysql> SELECT MAX(Number) FROM DemoTable WHERE Number < (SELECT MAX(Number) FROM DemoTable);
这将产生以下输出-
+-------------+ | MAX(Number) | +-------------+ | 92 | +-------------+ 1 row in set (0.00 sec)