How do I get the latest insert (by ID) from a table?
Get the latest insert
Started by Hajjel, Dec 04 2009 09:00 AM
12 replies to this topic
#1
Posted 04 December 2009 - 09:00 AM
|
|
|
#2
Posted 04 December 2009 - 09:03 AM
Can you offer a few more details? Are you talking about an HTML table, a database table, something else?
#3
Posted 04 December 2009 - 09:07 AM
WingedPanther said:
Can you offer a few more details? Are you talking about an HTML table, a database table, something else?
sry, a database table ofc :P
#4
Posted 04 December 2009 - 09:27 AM
With a database table, unless you are recording a timestamp value when the record is added, or using a trigger to log which is the most recent, you may not be able to.
#5
Posted 04 December 2009 - 09:44 AM
WingedPanther said:
Can you offer a few more details? Are you talking about an HTML table, a database table, something else?
#6
Posted 04 December 2009 - 12:02 PM
Yes, if you want the largest ID it is possible
Quote
SELECT * FROM `table` ORDER BY `id` DESC LIMIT 1
#7
Posted 04 December 2009 - 05:48 PM
You can sort it and grab it by ID, however, there might be some performance problems when you do this with a lot of data. Perhaps, a slightly better thing to do is store when the record was inserted and do this:
:) Though, I don't know if this is any better. I just see problems with sorting the entire table when you have A LOT of data.
SELECT * FROM table_name WHERE time_stamp = MAX('time')
:) Though, I don't know if this is any better. I just see problems with sorting the entire table when you have A LOT of data.
#8
Posted 04 December 2009 - 05:53 PM
if you're in mysql, you can use mysql_insert_id() if you use auto_increment as primary key... but it's not always reliable, as it is the very last query done, so if someone else does another insert, you might get that one instead....
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
I study Information Systems at Karlstad University when I'm not on CodeCall
#9
Guest_Jordan_*
Posted 04 December 2009 - 06:31 PM
Guest_Jordan_*
Orjan said:
if you're in mysql, you can use mysql_insert_id() if you use auto_increment as primary key... but it's not always reliable, as it is the very last query done, so if someone else does another insert, you might get that one instead....
This (the method above by Orjan) is the method you should use. Getting the highest ID or latest date will not work. When your site becomes popular and you have 13 people registering at the same time you'll have a lot of messed up IDs.
One thing Orjan is wrong about above is the reliability. mysql_insert_id() is per session. That means it will ONLY get the ID of the last insert statement during your mysql session. A session starts with the mysql connection statement and ends when you close the connection (or your PHP script ends). This means, if you have mutiple IDs being generated at the same time, it does not matter. This function will pull the last ID of the previous insert via that session only.
Hopefully that isn't confusing...
#10
Posted 04 December 2009 - 07:18 PM
ah okey, it's at least connection sensitive. but anyways. you can't do two inserts in a row and expect to have the first insert's id from the mysql_insert_id.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
I study Information Systems at Karlstad University when I'm not on CodeCall
#11
Posted 09 December 2009 - 08:50 AM
Quote
you can't do two inserts in a row and expect to have the first insert's id from the mysql_insert_id
#12
Posted 09 December 2009 - 09:17 AM
according to mysql documentation, there is no stated exceptions for any database types
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
I study Information Systems at Karlstad University when I'm not on CodeCall


Sign In
Create Account


Back to top










