How to get the last 5 entries from a mysql table?

Discussion in 'Programming' started by kuser, May 18, 2012.

  1. #1
    I have an mysql table containg some important notes.

    the problem is this: SQL it's not always writing always the last entry at the end of the table, if a free "cell" is found it will write the new entry even in the middle of the table.

    So, is there anyway i can extract the last 5 entered data?

    I store the "entered date" but, i have over 300,000 entries so far... and i will just slow down the database by parsing it all again and again for the "last 5 entries" ...

    Here is the problem :
    http://postimage.org/image/taz37oc29/

    see the ID? it's not always entering the data in order...
     
    Last edited: May 18, 2012
    kuser, May 18, 2012 IP
  2. rmacd

    rmacd Greenhorn

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #2
    Have you somehow managed to disable auto_increment on the field? IDs shouldn't be re-used.

    After you've checked that, LIMIT # should work on the query.
     
    rmacd, May 18, 2012 IP
  3. PK-Host

    PK-Host Guest

    Messages:
    109
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #3
    In the structure of that table ensure id is set to AUTO INCREMENT and Primary Key, this will let ensure all new rows are in order. As for your existing ones. Could you not write a code to cache the last 5 rows and call it upon request instead of returning it directly from the database?
     
    PK-Host, May 20, 2012 IP