等效于连接查询WHERE中的Max()

时间:2012-06-21 01:00:19

标签: mysql max

我希望能够写ORDER BY revision_id DESC LIMIT 1类似于Having Max(revision_id),但这并不像我想要的那样将它放在WHERE子句附近。将Max(revision_id)放在select中,返回修订版ID的最高值,但其余字段不应该与它一起使用(它在连接中是一个)。

查询:

SELECT * FROM field_data_field_city
LEFT JOIN profile on(field_data_field_city.entity_id = profile.pid)
LEFT JOIN users on(profile.uid = users.uid)
WHERE users.uid = 1 
ORDER BY revision_id DESC
LIMIT 1`

想要摆脱限制,以便我可以同时为多个用户执行此操作

我试过

GROUP BY users.uid
ORDER BY revision_id DESC

但我得到最低revision_id,我想要最高

2 个答案:

答案 0 :(得分:0)

你可以尝试下面的内容:

WHERE revision_id=(SELECT MAX(revision_id) FROM table)

答案 1 :(得分:0)

假设字段revision_id在您的表格配置文件中,您可以使用:

SELECT *
FROM field_data_field_city
LEFT JOIN profile ON field_data_field_city.entity_id = profile.pid
LEFT JOIN users ON profile.uid = users.uid
WHERE profile.revision_id IN (
    SELECT MAX(p.revision_id)
    FROM profile p
    GROUP BY p.uid) T;