Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

howto find the pk of the first and last record in a table

Status
Not open for further replies.

excession

Programmer
Sep 22, 2004
111
GB
Is there a simple way to find the first and the last record in a table?

Or perhaps some way to work through a table accessing each entry at a time?

I'm writing an app to migrate data from one very badly designed database to a new one. I need to read all the data in a table, one row at a time so I can write it to the new db.


Looking for a job as a programmer in Bristol UK.
 
To return the "first" (lowest primary-key value) record in a table, you can use:[tt]
SELECT * FROM tblname ORDER BY keyfldname LIMIT 0,1[/tt]
Then, the next one can be retrieved with:[tt]
SELECT * FROM tblname ORDER BY keyfldname LIMIT 1,1[/tt]
and so on.
The "last" record can be retrieved with:[tt]
SELECT * FROM tblname ORDER BY keyfldname DESC LIMIT 0,1[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top