1

I have a column in my mysql table something like this:

appVersion

5.16.0.2889
5.16.0.2893
5.14.0
5.14.0.3
5.12.0.1
5.15.0.14
5.1.0.2
5.12.0.3

I want to get the distinct latest versions of this column segregated on the basis of the first two numbers (a.b.c.d then only consider a.b as a single version)

like here we have let’s say the 3 distinct latest versions would be:

5.16.0.2893
5.16.0.2889
5.15.0.14
5.14.0.3
5.14.0

can someone please suggest mysql query for this?

definition of latest: let’s say if we have

5.14.0.9
5.12.0.4
6.1.0.2
5.14.0.12
5.16.1.2
5.12.1.1

distinct latest 3 would be

6.1.0.2
5.16.1.2
5.14.0.12
5.14.0.9
batman
  • 53
  • 8

2 Answers2

1

What I understand from your question, you need to display 3 latest "a.b" kind of versions, but with all it's subversions. Here is the code that will work :

     select ver from app inner join 
  (select CONCAT(LPAD(SUBSTRING_INDEX(ver, '.', 1), 50, '0'),
LPAD(SUBSTRING(ver, LOCATE('.', ver)+1, LOCATE('.', ver, LOCATE('.', ver)+1)), 50, '0')) as ver2
from app group by ver2 order by ver2 desc limit 3) as v2 
on CONCAT(LPAD(SUBSTRING_INDEX(ver, '.', 1), 50, '0'),
LPAD(SUBSTRING(ver, LOCATE('.', ver)+1, LOCATE('.', ver, LOCATE('.', ver)+1)), 50, '0')) = v2.ver2 
order by v2.ver2 desc

The inner query spits out the top 3 versions of the form 'a.0000b', and the inner join matches all the versions to 'a.b' = 'c.d'

Prakhar Londhe
  • 1,431
  • 1
  • 12
  • 26
0
SELECT VersNo
FROM (
SELECT  DISTINCT SUBSTRING_INDEX(appVersion,'.',2) AS 'VersionNo',appVersion AS 'VersNo'
FROM table
ORDER BY appVersion)s
ORDER BY VersNo;
Nimantha
  • 6,405
  • 6
  • 28
  • 69