查找内连接上具有MAX时间戳的记录

时间:2013-04-17 08:33:38

标签: mysql

我可能会看到这一切都错了,但我在MySQL中遇到了这个问题:我有一个可以有多个结果的表,我想获得最大时间戳。

目前我有这个问题:

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'

在上面,joinsheartbeat_activity可以有多个具有不同时间戳的记录,所以我想做类似的事情:

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

但是,在每个连接上执行此操作都很昂贵,因为它会在所有记录上进行选择。什么是最好的解决方法?

0 个答案:

没有答案