Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Last row in the database

Tags:

mysql

How do you get the last added record/row to the database in MySQL. I have to know this because I only want to update the last added row to the database.

like image 271
Niels Avatar asked Nov 21 '25 18:11

Niels


2 Answers

if you have an auto-incrementing id you can do this

select * from your_table order by id desc limit 1

or even simpler

select max(id) from your_table

generally if you have a column in your table that indicates what your last record is do

select max(column_that_indicates_order) from your_table

if you do not have any column indicating what your last record was then you can't find it. Just selecting the first element without any order will not give the lastest entry last.

Edit

To update your last record you can do this:

UPDATE tblPlaces SET lat = '%s', lng = '%s' 
order by id desc
limit 1
like image 78
juergen d Avatar answered Nov 23 '25 09:11

juergen d


Assuming you have an ID column within your table you would do this by:

SELECT id FROM table ORDER BY id DESC LIMIT 1;
like image 23
Rawkode Avatar answered Nov 23 '25 08:11

Rawkode



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!