这是我的表结构:
File | Version | Function
1 | 1 | 1
1 | 2 | 1
1 | 3 | 1
1 | 2 | 2
2 | 1 | 4
3 | 2 | 5
我只需要它返回这些行
1 | 3 | 1
2 | 1 | 4
3 | 2 | 5
意思是我只想要每个文件都有最新版本的函数。
我不想要下面的结果,即不是最新版本的唯一函数 ID
1 | 3 | 1
1 | 2 | 2
...
我看过How can I SELECT rows with MAX(Column value), DISTINCT by another column in SQL? ,但这会返回最新的唯一函数 ID。
查询需要与 sqlite3 兼容。
最佳答案
执行此操作的一种有效方法通常是使用 not exists
:
select t.*
from table t
where not exists (select 1
from table t2
where t2.file = t.file and t2.Version > t.version
);
此查询可以利用 table(file, version)
上的索引。
这将查询改写为:“从表中获取相应文件没有更大版本的所有行。”
关于SQL:过滤具有最大值的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24708109/