Niels
Niels

Reputation: 425

Last row in the database

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.

Upvotes: 2

Views: 15615

Answers (3)

Natnael Ghirma
Natnael Ghirma

Reputation: 528

This is the function for last inserted row when $conn, is the name of your connection. This is only good for last single row.

$thelastid=mysqli_insert_id($conn);

If you want to retrieve the last multiple rows, then you use select * from your_table order by id desc limit 1

Upvotes: 1

juergen d
juergen d

Reputation: 204924

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

Upvotes: 7

Rawkode
Rawkode

Reputation: 22592

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

SELECT id FROM table ORDER BY id DESC LIMIT 1;

Upvotes: 6

Related Questions