By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,985 Members | 1,776 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,985 IT Pros & Developers. It's quick & easy.

select records by latest date

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

Expand|Select|Wrap|Line Numbers
  1. timestamp  method  id comment
  2. 2009-01-10 getThud 16 "....."
  3. 2009-01-10 getFoo  12 "....."
  4. 2009-01-10 getBar  12 "....."
  5. 2009-01-11 getFoo  12 "....."
  6. 2009-01-11 getBar  12 "....."
desired results

Expand|Select|Wrap|Line Numbers
  1. timestamp  method  id comment
  2. 2009-01-10 getThud 16 "....."
  3. 2009-01-11 getFoo  12 "....."
  4. 2009-01-11 getBar  12 "....."
I used below code, iam not sure whether it's the right approach.

Expand|Select|Wrap|Line Numbers
  1. select max(timestamp),method,id from tables where 1 group by method,id order by timestamp desc 
May 4 '12 #1
Share this Question
Share on Google+
1 Reply


P: 45
wooooooooow, i tried this, it's working

Expand|Select|Wrap|Line Numbers
  1. SELECT a.`timestamp`,a.`method`,a.`id`,d.`studentname`
  2. FROM `schools` a
  3. INNER JOIN `schoolstudent` d ON a.`studentid`=d.`studentid`
  4. WHERE a.`pid` IN (
  5.     SELECT MAX(`pid`)
  6.     FROM `schools`
  7.     GROUP BY `method`,`id`,`order`
  8. ORDER BY a.`id` ASC;
  9.  
May 4 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.