I am attempting to return the data from the rows with the most recent date of each distinct candidate_id. It is correctly returning the most recent date (the created_unix column), but not the rest of the data from the corresponding row.
SELECT candidate_id, message, max(created_unix), jobpost_id, staffuserid
FROM messages
WHERE employer_id='$employerid' AND last='company'
GROUP BY candidate_id
You must
group byeverything not using an aggregate function:If your
messageis different per row and you want togroup by candidate_id, then you must not be usingmessage. In that case, simply remove it from your select list and you won’t need it in yourgroup bylist. The same goes for any other field you aren’t using.Remember, when using aggregate functions, you must contain each field in either an aggregate function or the
group by. Otherwise, SQL won’t know from which row to pull the data for the row returned.Update:
After seeing what you’re looking for, this will do the trick: