我有一个不包含唯一列的表

id | fid | ver | dir

1 || 1 || 1 || 3

2 || 2 || 1 || 2

3 || 3 || 1 || 3

4 || 3 || 2 || 3

5 || 4 || 1 || 4

6 || 5 || 1 || 5


我的问题是如何在dir = 3中选择最新fid的ID(基于ver)

我尝试了(SELECT id FROM table WHERE dir ='3' GROUP BY fid ORDER BY version ),但这给了我

id

1

3


这不是我想要得到的结果。这是我期望得到的结果:

id

1

4

最佳答案

select id
from myTable t1
where dir = 3
and not exists (
    select 1
    from myTable t2
    where t1.version < t2.version
    and t1.dir = t2.dir
    and t1.fid = t2.fid
)
order by version

关于mysql - MySQL组按降序排列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25228442/

10-14 18:44