By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,654 Members | 1,842 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,654 IT Pros & Developers. It's quick & easy.

reterieving the most recently added data from MySQL

P: 56
i want to get the most recently added data from MySQL
for example, if we have a table named test with column : id
the data is inserted as follows: 2, 4, 1, 3
when i type this query :
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM test ORDER BY id DESC LIMIT 1
, it returns 4
but i want a query that returns 3 (the last inserted data)
can i do this?
Feb 14 '14 #1

✓ answered by Rabbit

The auto increment should probably be the primary key. Any other keys should be a unique index. But if you don't want to do it that way, then use the timestamp field instead.

If you mean you want to do that query without changing the table at all, then it's impossible. You need a way to identify when the row was added so you can find the most recent. And to do that, you need to add a field because you currently have nothing to identify the most recent row.

Share this Question
Share on Google+
4 Replies


Rabbit
Expert Mod 10K+
P: 12,365
Use an auto increment field or include a column with a timestamp.
Feb 14 '14 #2

P: 56
i tried the auto increment field, but it makes me drop the primary key of the table and make that field the pk
i want the table as it is without changing anything in it
Feb 14 '14 #3

Rabbit
Expert Mod 10K+
P: 12,365
The auto increment should probably be the primary key. Any other keys should be a unique index. But if you don't want to do it that way, then use the timestamp field instead.

If you mean you want to do that query without changing the table at all, then it's impossible. You need a way to identify when the row was added so you can find the most recent. And to do that, you need to add a field because you currently have nothing to identify the most recent row.
Feb 15 '14 #4

P: 56
i used the timestamp field and it works correctly
thanks a lot :)
Feb 15 '14 #5

Post your reply

Sign in to post your reply or Sign up for a free account.