How can I select the row with the highest ID in MySQL?

后端 未结 9 2000
故里飘歌
故里飘歌 2020-12-24 11:06

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

SELECT * FROM permlog WHERE max(id)

Errors come up, can som

相关标签:
9条回答
  • 2020-12-24 11:24
    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.

    0 讨论(0)
  • 2020-12-24 11:25
    SELECT * FROM permlog ORDER BY id DESC LIMIT 0, 1
    
    0 讨论(0)
  • 2020-12-24 11:27

    For 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:

    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

    0 讨论(0)
提交回复
热议问题