I have the following tables:
Apps
TYPE_ID | BUILD_ID | CONFIG_ID | VERSION_ID | (All foreign keys to the respective tables)
1 | 1 | 1 | 1 |
1 | 1 | 1 | 2 |
2 | 2 | 3 | 3 |
2 | 2 | 3 | 4 |
Versions
ID | major | minor | patch
1 | 1 |0 |1
2 | 2 |0 |0
3 | 3 |0 |3
4 | 4 |0 |0
I need to select highest version rows from Apps
table for each unique combinations of TYPE_ID
, BUILD_ID
and CONFIG_ID
.
The version number should be calculated by MAX(major * 1000000 + minor * 1000 + patch)
in the versions table.
So from the given example of the Apps
table the result would be:
TYPE_ID | BUILD_ID | CONFIG_ID | VERSION_ID |
1 | 1 | 1 | 2 |
2 | 2 | 3 | 4 |
Have tried something like this:
SELECT p1.* FROM Apps p1
INNER JOIN (
SELECT max(VERSION_ID) MaxVersion, CONFIG_ID
FROM Apps
GROUP BY CONFIG_ID
) p2
ON p1.CONFIG_ID = p2.CONFIG_ID
AND p1.VERSION_ID = p2.MaxVersion
GROUP BY `TYPE_ID`, `BUILD_ID`, `CONFIG_ID`
But MAX
is applied on the VERSION_ID
and I need MAX
to be applied on major
, minor
and patch
combinations.
MySQL Version 15.1 distribution 5.5.56-MariaDB
Any help would be appreciated.
Cheers!