Click to See Complete Forum and Search --> : How to select the Last from a table..using Mysql


mishu007
07-26-2006, 02:42 AM
Suppose this is a Table

Create Table Person(
PersonID varchar(8),
PersonLN varchar(16),
PersonFN varchar(16),
PersonADD varchar(64),
PersonPH varchar(16));

so there are many enrty in that table.. So how to select the last entry from that table? whats the Mysql statement?

john_de116
07-26-2006, 03:29 AM
It is not a correct table. If suposed, you entered the data into PersonID like AB100001,AB100002,AB100003,...... You can write the query after the 'where caluse ' that is "order by PersonID desc". If you do not write like that, add a datetime field. If you don't do these two things, you can't sort the table and It is not a correct table.

mishu007
07-26-2006, 03:35 AM
well actally there are lots more into that table.. i just split that.. I dont think whether it is correct or not. what i have to need the Most recent entry into that table.. only that one, nothing else.. take PersonID as a Primary key. so what will be the MYSQL statement...

NogDog
07-26-2006, 03:47 AM
The issue is that you need to be able to sort the table on at least one column that contains sequential data. It could be a datetime or timestamp field, or an auto-incremented integer; but there is no "select the last row entered" SQL function. If, in your example, PersonID is an auto-incremented integer, you could get the last record entered with:

SELECT * FROM `Table` ORDER BY `PersonID` DESC LIMIT 1;

mishu007
07-26-2006, 04:14 AM
can i select the last row data using Time field?

john_de116
07-26-2006, 04:44 AM
Ya, you can select the the lst row using time field.......
SELECT * FROM `Person` ORDER BY `timefield` DESC limit 0,1;

mishu007
07-26-2006, 02:16 PM
thanks everyone... its working......