SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    CA
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Selecting last 3 records?

    Hi,

    I'm quite the newbie, but can anyone tell me how I to create a MySQL query that returns only the last three entries in a database?

    Thanks.

  2. #2
    Free Geek computerages's Avatar
    Join Date
    Oct 2004
    Location
    /dev/null
    Posts
    1,071
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jjfletch
    Hi,

    I'm quite the newbie, but can anyone tell me how I to create a MySQL query that returns only the last three entries in a database?

    Thanks.
    Code:
    SELECT * FROM table ORDER BY ID DESC LIMIT 3

  3. #3
    SitePoint Wizard guelphdad's Avatar
    Join Date
    Oct 2003
    Location
    St. Catharines, ON Canada
    Posts
    1,708
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That answer is not correct. It is only correct if there is a column labeled ID and the last three items are assumed to be the ones with the largest ID values.

    There is no way to determine last three items as this is a database and not flat file system. if you used a timestamp for instance then you could get three latest by timestamp. but otherwise the data is not put in the database in any one order. You would have to define more specifically on what criteria (such as the ID criteria the user suggested above, but that one would have to have an auto increment ID to work correctly).


Bookmarks

Posting Permissions

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