有没有办法在一个SQL查询中执行此操作?我想用各种语言获得相同条目的最新(版本)。
我的表和示例数据是:
ID | Index | Version | Language | ...
------------------------------------------------
1 1 1 1 test1
2 1 2 1 test2
3 2 1 1 test3
4 2 1 2 test4
5 2 1 3 test5
6 2 2 3 test6
7 3 1 1 test7
8 3 1 2 test8
我真的无法想出在一个查询中获得此功能的方法。但也许它可行吗?
我想得到这样的东西:
ID | Index | Version | Language | ...
------------------------------------------------
2 1 2 1 test2
3 2 1 1 test3
4 2 1 2 test4
6 2 2 3 test6
7 3 1 1 test7
8 3 1 2 test8
test1已经用完了,因为test2使用的是同一种语言并且它是新的 test5已经用完,因为test6在同一种语言中更新
SELECT T1.* FROM entry_view T1
LEFT JOIN entry_view T2 ON (T1.version < T2.version AND T1.id_language = T2.id_language AND T1.`index` = T2.`index`)
WHERE T2.ID IS NULL
答案 0 :(得分:4)
SELECT T1.ID, T1.Index, T1.Version, T1.Language FROM Table T1
LEFT JOIN Table T2 ON (T1.ID < T2.ID AND T1.Language = T2.Language)
WHERE T2.ID IS NULL
编辑:我假设ID正在增加,最新的ID将是最后一个。
也许试试这个:
SELECT T1.ID, T1.Index, T1.Version, T1.Language FROM Table T1
LEFT JOIN Table T2 ON (T1.Version < T2.Version AND T1.Language = T2.Language)
WHERE T2.ID IS NULL
答案 1 :(得分:1)
SELECT t.*
FROM atable t
INNER JOIN (
SELECT
Index,
MAX(Version) AS MaxVersion,
Language
FROM atable
GROUP BY Index, Language
) max ON t.Index = max.Index AND t.Version = max.MaxVersion
AND t.Language = max.Language
答案 2 :(得分:0)
那怎么样?
SELECT S.*
FROM (
SELECT T1.*
FROM entry_view T1
ORDER BY T1.version DESC
) S
GROUP BY language