您可以在ORDER BY和LIMIT OFFSET的帮助下选择MySQL中ID最高的行
语法如下-
select *from yourTableName order by yourColumnName desc limit 1 offset 0;
为了理解上述语法,让我们创建一个表。创建表的查询如下-
mysql> create table HighestIdOrderBy −> ( −> EmployeeId int, −> EmployeeName varchar(200) −> );
借助insert命令将记录插入表中。查询如下-
mysql> insert into HighestIdOrderBy values(200,'David'); mysql> insert into HighestIdOrderBy values(1000,'Bob'); mysql> insert into HighestIdOrderBy values(600,'John'); mysql> insert into HighestIdOrderBy values(300,'Johnson'); mysql> insert into HighestIdOrderBy values(100,'Carol');
使用select语句显示表中的所有记录。查询如下-
mysql> select *from HighestIdOrderBy;
以下是输出-
+------------+--------------+ | EmployeeId | EmployeeName | +------------+--------------+ | 200 | David | | 1000 | Bob | | 600 | John | | 300 | Johnson | | 100 | Carol | +------------+--------------+ 5 rows in set (0.00 sec)
这是从MySQL表中选择最大ID的查询。查询如下-
mysql> select *from HighestIdOrderBy order by EmployeeId desc limit 1 offset 0;
以下是输出-
+------------+--------------+ | EmployeeId | EmployeeName | +------------+--------------+ | 1000 | Bob | +------------+--------------+ 1 row in set (0.00 sec)