1

I may be looking at this all wrong, but I have this problem in MySQL: I have a table that can have multiple results and I want to get the max timestamp.

I have this query at present:

LEFT JOIN heartbeat_activity hbco ON jid.field_application_job_id_nid = hbco.nid_target
AND hbco.uid_target = n.uid
AND hbco.message_id = 'heartbeat_call_in_confirm'
LEFT JOIN heartbeat_activity hbci ON jid.field_application_job_id_nid = hbci.nid_target
AND hbci.uid_target = n.uid
AND hbci.message_id = 'heartbeat_call_in_requested'
LEFT JOIN heartbeat_activity hbrj ON jid.field_application_job_id_nid = hbrj.nid_target
AND hbrj.uid_target = n.uid
AND hbrj.message_id = 'heartbeat_call_in_rejected'

On the above, joins through heartbeat_activity can have multiple records with differing timestamps, so I would like to do something like:

SELECT DISTINCT MAX(TIMESTAMP) AS TIMESTAMP,
                nid_target,
                uid_target
FROM heartbeat_activity
WHERE message_id = 'heartbeat_call_in_confirm'
GROUP BY uaid

But doing this on each join is expensive, as it's selecting on all the records. What's the best way of going about this?

4

0 回答 0