select records by latest date

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • charles07
    New Member
    • Dec 2011
    • 45

    select records by latest date

    how can i get records by the latest date. ie. select last inserted records. mysql db structure below

    Code:
    timestamp  method  id comment
    2009-01-10 getThud 16 "....."
    2009-01-10 getFoo  12 "....."
    2009-01-10 getBar  12 "....."
    2009-01-11 getFoo  12 "....."
    2009-01-11 getBar  12 "....."
    desired results

    Code:
    timestamp  method  id comment
    2009-01-10 getThud 16 "....."
    2009-01-11 getFoo  12 "....."
    2009-01-11 getBar  12 "....."
    I used below code, iam not sure whether it's the right approach.

    Code:
    select max(timestamp),method,id from tables where 1 group by method,id order by timestamp desc
  • charles07
    New Member
    • Dec 2011
    • 45

    #2
    wooooooooow, i tried this, it's working

    Code:
    SELECT a.`timestamp`,a.`method`,a.`id`,d.`studentname`
    FROM `schools` a
    INNER JOIN `schoolstudent` d ON a.`studentid`=d.`studentid`
    WHERE a.`pid` IN (
        SELECT MAX(`pid`)
        FROM `schools`
        GROUP BY `method`,`id`,`order`
    ) 
    ORDER BY a.`id` ASC;

    Comment

    Working...