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

后端 未结 9 1999
故里飘歌
故里飘歌 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:11

    SELECT MAX(id) FROM TABELNAME

    This identifies the largest id and returns the value

    0 讨论(0)
  • 2020-12-24 11:11

    Suppose you have mulitple record for same date or leave_type but different id and you want the maximum no of id for same date or leave_type as i also sucked with this issue, so Yes you can do it with the following query:

    select * from tabel_name where employee_no='123' and id=(
       select max(id) from table_name where employee_no='123' and leave_type='5'
    )
    
    0 讨论(0)
  • 2020-12-24 11:13

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

    SELECT MAX(ID) FROM tablename
    
    0 讨论(0)
  • 2020-12-24 11:17

    This is the only proposed method who actually selects the whole row, not only the max(id) field. It uses a subquery

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

    0 讨论(0)
  • 2020-12-24 11:23
    SELECT MAX(ID) FROM tablename LIMIT 1
    

    Use this query to find the highest ID in the MySQL table.

    0 讨论(0)
  • 2020-12-24 11:23
    SELECT * FROM `permlog` as one
    RIGHT JOIN (SELECT MAX(id) as max_id FROM `permlog`) as two 
    ON one.id = two.max_id
    
    0 讨论(0)
提交回复
热议问题