mysql query the latest date

Posted by user295189 on Stack Overflow See other posts from Stack Overflow or by user295189
Published on 2010-04-29T20:03:01Z Indexed on 2010/04/29 20:07 UTC
Read the original article Hit count: 432

Filed under:
|
|
|
|

I am running this query

SEL

ECT
 sh.*,
 u.initials AS initals
FROM
 database1.table1 AS sh
JOIN
 database2.user AS u
ON
 u.userID = sh.userid
WHERE
 id = 123456

AND 
dts = ( SELECT MAX(dts) from database1.table1 )

ORDER BY
 sort_by, category

In the table1 I have records like this

dts                           status                    category         sort_by
2010-04-29 12:20:27        Civil Engineers          Occupation 1 
2010-04-28 12:20:27        Civil Engineers          Occupation 1 
2010-04-28 12:20:54 Married             Marital Status 2      2010-04-28 12:21:15 Smoker             Tobbaco    3
2010-04-27 12:20:27        Civil Engineers          Occupation 1 
2010-04-27 12:20:54 Married             Marital Status 2      2010-04-27 12:21:15 Smoker             Tobbaco    3
2010-04-26 12:20:27        Civil Engineers          Occupation 1 
2010-04-26 12:20:54 Married             Marital Status 2      2010-04-26 12:21:15 Smoker             Tobbaco    3

so if you look at my data, I am choosing the latest entry by category and sort_id. however in some case such as on 29th (2010-04-29 12:20:27) I have only one record. So in this case I want to show occupation for latest and then the rest of them (latest). But currently it displays only one row. Thanks

© Stack Overflow or respective owner

Related posts about php

Related posts about php5