- I en afledt tabel fås den maksimale versionsværdi for hver type_id
- Forbind tilbage til hovedtabellen for at få den tilsvarende række.
Prøv følgende:
SELECT e.*
FROM
example1 AS e
JOIN
(
SELECT type_id,
MAX(version) AS maximum_version
FROM example1
WHERE class_id = 10
GROUP BY type_id
) AS dt ON dt.type_id = e.type_id AND
dt.maximum_version = e.version
WHERE e.class_id = 10
Resultat
| id | version | text | class_id | type_id |
| --- | ------- | ----- | -------- | ------- |
| 1 | 1 | text1 | 10 | 1 |
| 2 | 1 | text2 | 10 | 2 |
| 4 | 1 | test4 | 10 | 4 |
| 9 | 3 | last | 10 | 3 |
| 11 | 3 | rrrr | 10 | 5 |