开发者

Selecting last version of entry in different language from sql database

开发者 https://www.devze.com 2023-02-13 05:55 出处:网络
Is there any way to do this in one sql query? I would like to get the newest (version) of the same entry in every language available.

Is there any way to do this in one sql query? I would like to get the newest (version) of the same entry in every language available.

My table and example data is:

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 is unique identifer
  • Index is unique for any entry in database which means that all time versions and language version of the same article/entry share the same Index.
  • Version is obviously version number. 1 is the first version and any greater number is usually newer.
  • Language is the number of language version of entry/article.

I really can't think of way to get this in one query. But maybe it's feasible?

I would like to get something like this:

ID  |  Index  |  Version  |  Language  | ...
------------------------------------------------
2        1          2            1         test2
3        2          1            1         test3
4        2          1            2         test开发者_运维知识库4
6        2          2            3         test6
7        3          1            1         test7
8        3          1            2         test8

test1 is out because test2 is in the same language and it's newer

test5 is out because test6 is newer in the same language

Resolved

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


    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

Edit: i assumed the IDs were increasing and the newest one would be the last.

maybe try this:

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


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


And what about SELECT S.* FROM ( SELECT T1.* FROM entry_view T1 ORDER BY T1.version DESC ) S GROUP BY language

0

精彩评论

暂无评论...
验证码 换一张
取 消