Fetching last 5 Records

Discussion in 'MySQL' started by benson647, Oct 11, 2009.

  1. #1
    Hello All

    I have a mysql table and I want to fetch last inserted five records from the table. Can anyone tell me how can I do this using query?
     
    benson647, Oct 11, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Generally you would use:

    SELECT column_names FROM my_table ORDER BY id_column DESC LIMIT 5;
     
    jestep, Oct 11, 2009 IP
  3. phpdoctor22

    phpdoctor22 Peon

    Messages:
    96
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You need to count the number of rows, then LIMIT $count, 5
     
    phpdoctor22, Oct 12, 2009 IP
  4. phpdoctor22

    phpdoctor22 Peon

    Messages:
    96
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Sorry I forgot to add subtract 5 from count. So $count = $count -5;
     
    phpdoctor22, Oct 12, 2009 IP
  5. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #5
    SELECT column_names FROM my_table ORDER BY id_column LIMIT COUNT(id_column) - 5, 5
    Code (markup):
    Something like that should do it
     
    JAY6390, Oct 13, 2009 IP
  6. phones2me

    phones2me Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    provided you have an integer id column that increments with every insertion

    otherwise tag each row with a date and sort on that
     
    phones2me, Oct 14, 2009 IP
  7. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #7
    COUNT works regardless of the id being an integer, varchar or otherwise
     
    JAY6390, Oct 14, 2009 IP
  8. phones2me

    phones2me Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    ordering however doesnt, especially if the column doesnt exist

    all the solutions above assume the existance of this column
     
    phones2me, Oct 14, 2009 IP
  9. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #9
    If there is no ID column or a date column there would be no way to do any date based or logical ordering at all. In most situations it would be irrational to design a database table without some chronological identifier.
     
    jestep, Oct 14, 2009 IP
  10. phones2me

    phones2me Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    exactly

    in my experience, making assumptions about 'most situations' leads to problems
     
    phones2me, Oct 14, 2009 IP
  11. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #11
    "I want to fetch last inserted five records"

    I don't know about you guys, but that infers a need for a "chronological identifier" to me.
     
    joebert, Oct 19, 2009 IP