Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2003
    Location
    Ohio/Chicago
    Posts
    75

    Unanswered: query: most recent events

    The table has an autoincrementing key.

    I am currently doing this all from the server side where i query the DB and get the max number in the autoincr field, and then query again grabbing the most recent 15 from the max value i got earlier.

    I assume there's gotta be a better way to get the most recent 15 entries in the DB in one query, anyone know how?

  2. #2
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    select top 15 *
    from [yourtable]
    order by [autoincrementfield] desc

  3. #3
    Join Date
    Jul 2003
    Location
    Ohio/Chicago
    Posts
    75
    thank you once again blindman

  4. #4
    Join Date
    Jul 2003
    Location
    Ohio/Chicago
    Posts
    75
    ooh, wait, this grabs the first ones inserted into the DB (lowest numbers), i need the highest number, what's they keyword for that?

  5. #5
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    Make sure you order the recordset in descending order:

    order by [autoincrementfield] desc

    This will give you the 15 records with the highest values in the autoincrementfield.

    blindman

  6. #6
    Join Date
    Jul 2003
    Location
    Ohio/Chicago
    Posts
    75
    Haha, oops, thanks.

    but as a sidenote is there no opposite keyword of TOP?

  7. #7
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    No. You have to specify the order of the data. Another drawback of TOP is that you can't pass it a variable (or field value). You have to hard-code the number of rows you want to return or use dynamic sql statements.

    blindman

Posting Permissions

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