DB Question

Results 1 to 3 of 3

Thread: DB Question

  1. #1
    Join Date
    Dec 1969

    Default DB Question

    Is there a way I can select the last row of a table in SQL?

  2. #2
    Join Date
    Dec 1969

    Default depends

    What is the last row? You have to define what the last row is, a relational database does not have something as roworder.<BR><BR>If you have an autoincrement Id field you could do something like this:<BR><BR>SELECT TOP 1 * FROM TheTable ORDER BY IdField DESC<BR><BR>or if you have a &#039;DateAdded&#039; field:<BR><BR>SELECT TOP 1 * FROM TheTable ORDER BY DateAdded DESC<BR><BR>

  3. #3
    Join Date
    Dec 1969

    Default Trivially

    But first, you have to define which row *is* the last row.<BR><BR>Databases don&#039;t store records in any particular order, you know.<BR><BR>It is only via the ORDER BY clause in a query that you can *tell* SQL which way to "sort" the records, so that first and last records have meaning.<BR><BR>Anyway:<BR><BR>SELECT TOP 1 * FROM table ORDER BY whatever DESC<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts