Skip to main content
October 6, 2008
Question

How to get last record in query

  • October 6, 2008
  • 7 replies
  • 2276 views
What's the proper SQL syntax to get the last record in a table.
    This topic has been closed for replies.

    7 replies

    trubel
    Participating Frequently
    October 7, 2008
    The best way if you are using MS SQL is;

    Select MAX(bulletin_id) as LastRecord
    From (your tablename)
    Participating Frequently
    October 7, 2008
    quote:

    Select MAX(bulletin_id) as LastRecord....
    This would return the last bulletin_id, not the last entire record.... Dan's suggestion is more likely to give the correct results.

    Phil
    Inspiring
    October 6, 2008
    select somefields
    from sometables
    where somefile = (select max(somefield)
    from sometable)
    etc
    Inspiring
    October 6, 2008
    SELECT MAX(primary_key_field) AS MaxID
    FROM some_table

    --
    Ken Ford
    Adobe Community Expert Dreamweaver/ColdFusion
    Adobe Certified Expert - Dreamweaver CS3
    Adobe Certified Expert - ColdFusion 8
    Fordwebs, LLC
    http://www.fordwebs.com


    "PopcornCoder" <webforumsuser@macromedia.com> wrote in message
    news:gcd9rq$8t3$1@forums.macromedia.com...
    > What's the proper SQL syntax to get the last record in a table.

    Participating Frequently
    October 6, 2008
    If bulletin_id is an autonumber column, then you can sort it by bulletin_id desc and limit 1. That will give you the row with the highest bulletin_id.

    select * from foo_table
    order by bulletin_id desc limit 1
    October 6, 2008
    I am going to sort it based on bulletin_id which is an auto numbering column and I want to get another column called bulletin_number.
    Participating Frequently
    October 6, 2008
    -- duplicate post
    Participating Frequently
    October 6, 2008
    Depends on the contents of the table.