我们将首先按DESC排序,然后获取与最大id相关的值-
select *from yourTableName order by yourColumnName DESC LIMIT 1,1;
让我们首先创建一个表-
mysql> create table DemoTable -> ( -> Alldata int -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values(303); mysql> insert into DemoTable values(560); mysql> insert into DemoTable values(100); mysql> insert into DemoTable values(490); mysql> insert into DemoTable values(498);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
输出结果
+---------+ | Alldata | +---------+ | 303 | | 560 | | 100 | | 490 | | 498 | +---------+ 5 rows in set (0.00 sec)
以下是获取与最大id关联的记录的查询-
mysql> select *from DemoTable order by Alldata DESC LIMIT 1,1;
输出结果
+---------+ | Alldata | +---------+ | 498 | +---------+ 1 row in set (0.00 sec)