Last row in the database

北战南征 提交于 2019-12-12 19:12:07

问题


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


回答1:


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

SELECT id FROM table ORDER BY id DESC LIMIT 1;



回答2:


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



回答3:


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



来源:https://stackoverflow.com/questions/10550205/last-row-in-the-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!