Possible Duplicate:
MySQL retrieve latest record for Group
I have a query that groups all records that have the same information in the ‘address1’ field, unless they have a different jobType.
SELECT *
FROM jobs
WHERE client_id = '".$id."'
AND status = '3'
GROUP BY CASE
WHEN jobType IN ( '0', '3', '6' ) THEN '0'
ELSE jobType
end,
address1
ORDER BY Max(id) ASC
The problem I’m running into, is that the query always pulls the first instance in the group. I need it to pull the latest instance in the group. I’ve tried a few variations of ORDER BY, and that hasn’t seemed to do the trick.
Any help would be greatly appreciated.
You want the groupwise maximum:
However, this won’t be very index-friendly. Better to create a column that contains the result of your
CASEexpression (and then index that).