Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2001
    Location
    South Africa
    Posts
    33

    Unanswered: getting last 20 rows from a table

    Hi all,

    How can I get the last 20 rows from a table in 1 sql query ??

    Thanks

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    select foo, bar
    from yourtable
    order by foo ascending
    limit 20

    the above makes two assumptions

    1. foo is the field you want to use to determine sequence

    2. "last" means lowest


    rudy
    http://r937.com/

  3. #3
    Join Date
    Nov 2001
    Location
    South Africa
    Posts
    33
    sorry - what I wanted was :

    table
    _____
    1
    3
    4
    5
    6
    7
    8
    9
    0

    and I wanted say :

    table
    _____
    7
    8
    9
    0

    ??

    Thanks

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    what is the name of the table? what is the name of the column?

  5. #5
    Join Date
    Nov 2001
    Location
    South Africa
    Posts
    33
    table call statistics and column called log_datetime

  6. #6
    Join Date
    Nov 2001
    Location
    South Africa
    Posts
    33
    table called statistics and column called log_datetime

  7. #7
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    select log_datetime
    from statistics
    order by log_datetime descending
    limit 20

    this will give you
    9
    8
    7
    6
    ...


    if you want them in the other order than what was used to select them, you will have to re-sort them, either in a temp table or in your script (php or whatever)


    rudy

Posting Permissions

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