Jump to content

Best Way To Retrieve Last Row


frustrated

Recommended Posts

Hello,

I'm trying to figure out what is the best or most optimzed way to go about the following situation. All I want to do is retrieve the last row inserted into a table. It happens that one of the fields in my table has a date field, so my plan was to do something like this.

[code]
$sql = "SELECT id, name, date FROM the_table ORDER BY date ASC LIMIT 1";
[/code]

So I think that's pretty self explanatory but I'm sure that's probably not the best way to go about to retrieving the last record.

Anyone have any feedback on this topic for me?
Link to comment
Share on other sites

[!--quoteo(post=339396:date=Jan 24 2006, 04:10 PM:name=frustrated)--][div class=\'quotetop\']QUOTE(frustrated @ Jan 24 2006, 04:10 PM) [snapback]339396[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I'm trying to figure out what is the best or most optimzed way to go about the following situation. All I want to do is retrieve the last row inserted into a table. It happens that one of the fields in my table has a date field, so my plan was to do something like this.
[code]
$sql = "SELECT id, name, date FROM the_table ORDER BY date ASC LIMIT 1";
[/code]
[/quote]
If you want the last inserted row you probably should use descending order on date:
[code]
$sql = "SELECT id, name, date FROM the_table ORDER BY date DESC LIMIT 1";
[/code]
Link to comment
Share on other sites

First, if you're just inserted this row, you can use LAST_INSERT_ID() to get that uid and do a quick lookup. Second, if this is not the case, you should be using the UID field to find the newest record -- just realize that this is _not_ a safe way to find the next uid, since this is not an ACID operation.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.