How to select the last record from a MySQL table using SQL syntax

I have a table with several records. There is an id field. I would like to select the record with the most recent id (i.e. the highest id).

Any ideas?

+48
sql mysql
Apr 17 '10 at 17:12
source share
6 answers
SELECT * FROM table_name ORDER BY id DESC LIMIT 1 
+116
Apr 17 '10 at 17:14
source share

User order with desc :

 select * from t order by id desc limit 1 
+8
Apr 17 '10 at 17:13
source share

You can also do something like this:

 SELECT tb1.* FROM Table tb1 WHERE id = (SELECT MAX(tb2.id) FROM Table tb2); 

This is useful when you want to make multiple connections.

+7
Apr 29 '15 at 13:45
source share
 SELECT MAX("field name") AS ("primary key") FROM ("table name") 

Example:

 SELECT MAX(brand) AS brandid FROM brand_tbl 
+3
Sep 26 2018-11-11T00:
source share
 SELECT * FROM table ORDER BY id DESC LIMIT 0, 1 
+2
Apr 17 '10 at 17:15
source share

I used the following two:

 1 - select id from table_name where id = (select MAX(id) from table_name) 2 - select id from table_name order by id desc limit 0, 1 
+1
Sep 20 '17 at 10:44 on
source share



All Articles