quadrant6
12-09-2006, 10:34 PM
Firstly table structure - 1 user has multiple status records. When a new status record is created it gets the current datetime inserted into 'date_created'.
user
user_id
site_id
status
status
notes
date_created
I wish to display a list of all users and show beside them their current/most recent status record. Here is my code currently:
SELECT user.*, status.date_created, status.status, status.notes,
DATE_FORMAT(status.date_created,'%l:%i %p') as last_updated_time,
DATE_FORMAT(status.date_created,'%e/%c') as last_updated_date
FROM user
LEFT JOIN status ON status.user_id = user.user_id
WHERE user.site_id = 1
GROUP BY user.user_id
ORDER BY
CASE user.user_id WHEN 8 THEN 0 ELSE user.user_id END
Currently via the LEFT JOIN and GROUP this does display a list of all users with 1 status record per user but it's simply the first status record in the table (ordering by id i guess), not the most recent.
How can I get it to display the most recent instead, based on date_created. I've tried adding ordering to the left join but this returned an error.
:confused:
user
user_id
site_id
status
status
notes
date_created
I wish to display a list of all users and show beside them their current/most recent status record. Here is my code currently:
SELECT user.*, status.date_created, status.status, status.notes,
DATE_FORMAT(status.date_created,'%l:%i %p') as last_updated_time,
DATE_FORMAT(status.date_created,'%e/%c') as last_updated_date
FROM user
LEFT JOIN status ON status.user_id = user.user_id
WHERE user.site_id = 1
GROUP BY user.user_id
ORDER BY
CASE user.user_id WHEN 8 THEN 0 ELSE user.user_id END
Currently via the LEFT JOIN and GROUP this does display a list of all users with 1 status record per user but it's simply the first status record in the table (ordering by id i guess), not the most recent.
How can I get it to display the most recent instead, based on date_created. I've tried adding ordering to the left join but this returned an error.
:confused: