Jump to content

Get the latest insert

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
12 replies to this topic

#1
Hajjel

Hajjel

    Newbie

  • Members
  • PipPip
  • 22 posts
How do I get the latest insert (by ID) from a table?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Can you offer a few more details? Are you talking about an HTML table, a database table, something else?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Hajjel

Hajjel

    Newbie

  • Members
  • PipPip
  • 22 posts

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
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
Hajjel

Hajjel

    Newbie

  • Members
  • PipPip
  • 22 posts

WingedPanther said:

Can you offer a few more details? Are you talking about an HTML table, a database table, something else?
Can't I just get it from the largest ID? I save ID as int.

#6
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
Yes, if you want the largest ID it is possible

Quote

SELECT * FROM `table` ORDER BY `id` DESC LIMIT 1


#7
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
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:

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
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
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

#9
Guest_Jordan_*

Guest_Jordan_*
  • Guests

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
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
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

#11
sastro

sastro

    Newbie

  • Members
  • PipPip
  • 12 posts

Quote

you can't do two inserts in a row and expect to have the first insert's id from the mysql_insert_id
Is it possible with innoDB?

#12
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
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