Reputation: 723
I have a table with thousands of records, Now the problem is I want to get last record from the table. I don't know how to get, Can you please help us
Upvotes: 0
Views: 280
Reputation: 4158
Largely a duplicate of
How do I fetch the last record in a MySQL database table using PHP?
Alternatively if you mean how do I get the last record in the order that the records were created, add a field say with an (ref INTEGER NOT NULL AUTO_INCREMENT UNIQUE KEY)
Upvotes: 0
Reputation: 2488
Yuu could do this if you can't order by
Declare @Total int
SET @Total = SELECT COUNT(ID) FROM TABLE
To get the total number of records And then doing a
SELECT * FROM TABLE WHERE RowNumber = @Total
Upvotes: 0
Reputation: 5064
I hope it will help you
20.8.10.3. How to Get the Unique ID for the Last Inserted Row http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html
Upvotes: 0
Reputation: 10517
it's better to have some field e.g. creation_date and do the following
select * from mytable order by creation_date desc limit 1
Upvotes: 2
Reputation: 7712
Well if the table is designed correctly, there should be some sort of identification field. This should be an auto number unless your using some sort of guid.
you could use
SELECT MAX(id) FROM table
that would give you the most recent record entered into the table chronologicaly.
Upvotes: 0
Reputation: 2182
You could use ORDER BY in the select statement and order DESCENDING to put the results you want at the top of the select. Then use SELECT TOP 1.
EG:
SELECT TOP 1 * FROM Table ORDER BY Field DESC
Upvotes: 0