Results 1 to 10 of 10

Thread: Select LAST record in DB!??????

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216

    Question Select LAST record in DB!??????

    How the heck in a SQL statement can I select the LAST record in my DB?
    thanks

  2. #2
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818
    There isn't a SQL command for getting the last record.

    The only way would be to grab the entire collection and use .MOVELAST.

  3. #3
    RoyceWindsor1
    Guest
    Code:
    SELECT * FROM tablename WHERE key_column = (SELECT MAX(key_column) FROM tablename)

  4. #4
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818
    That will work, as long as you have got unique keys.

  5. #5
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818
    Actually that won't work

    the MAX clause is quantitive, and not chronological, therefore you would only return the record with the highest value, and not necessarily the last record.

    If you have a time stamped field you could use the MAX clause on this to achieve the desired effect.

  6. #6
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    go with the MOVELAST command. that will always work.
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  7. #7
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    This will work in SQL Server, Access and Sybase:
    Code:
    Select Top 1 * From MyTable Order By FieldId Desc

  8. #8
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    ahh good thinking serge.... TOP with a sort by Desc(ending) would work perfectly.
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  9. #9
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818
    I bow to your experience o wise one

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    Originally posted by Serge
    This will work in SQL Server, Access and Sybase:
    Code:
    Select Top 1 * From MyTable Order By FieldId Desc
    thank you all for your comments. They all work especially Serge's idea!


Posting Permissions

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



Click Here to Expand Forum to Full Width