如何在MySQL中选择ID最高的行?

时间:2020-12-18 13:18:25

How can I select the row with the highest ID in MySQL? This is my current code:

如何在MySQL中选择ID最高的行?这是我目前的代码:

SELECT * FROM permlog WHERE max(id)

Errors come up, can someone help me?

错误出现了,有人可以帮助我吗?

4 个解决方案

#1


51  

SELECT * FROM permlog ORDER BY id DESC LIMIT 0, 1

#2


19  

For MySQL:

对于MySQL:

SELECT *
FROM permlog
ORDER BY id DESC
LIMIT 1

You want to sort the rows from highest to lowest id, hence the ORDER BY id DESC. Then you just want the first one so LIMIT 1:

您希望对从最高到最低ID的行进行排序,因此ORDER BY ID为DESC。然后你只想要第一个如此LIMIT 1:

The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement.
[...]
With one argument, the value specifies the number of rows to return from the beginning of the result set

LIMIT子句可用于约束SELECT语句返回的行数。 [...]使用一个参数,该值指定从结果集的开头返回的行数

#3


16  

if it's just the highest ID you want. and ID is unique/auto_increment:

如果它只是你想要的最高ID。和ID是唯一的/ auto_increment:

SELECT MAX(ID) FROM tablename

#4


13  

SELECT *
FROM permlog
WHERE id = ( SELECT MAX(id) FROM permlog ) ;

This would return all rows with highest id, in case id column is not constrained to be unique.

这将返回具有最高id的所有行,以防id列不被约束为唯一。

#1


51  

SELECT * FROM permlog ORDER BY id DESC LIMIT 0, 1

#2


19  

For MySQL:

对于MySQL:

SELECT *
FROM permlog
ORDER BY id DESC
LIMIT 1

You want to sort the rows from highest to lowest id, hence the ORDER BY id DESC. Then you just want the first one so LIMIT 1:

您希望对从最高到最低ID的行进行排序,因此ORDER BY ID为DESC。然后你只想要第一个如此LIMIT 1:

The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement.
[...]
With one argument, the value specifies the number of rows to return from the beginning of the result set

LIMIT子句可用于约束SELECT语句返回的行数。 [...]使用一个参数,该值指定从结果集的开头返回的行数

#3


16  

if it's just the highest ID you want. and ID is unique/auto_increment:

如果它只是你想要的最高ID。和ID是唯一的/ auto_increment:

SELECT MAX(ID) FROM tablename

#4


13  

SELECT *
FROM permlog
WHERE id = ( SELECT MAX(id) FROM permlog ) ;

This would return all rows with highest id, in case id column is not constrained to be unique.

这将返回具有最高id的所有行,以防id列不被约束为唯一。