Client - Server 에서 앱 버전 체크하는 API 에서 사용할 만한 query.
버전이 최대 3자리라고 가정하고...
로직은 이렇게.....
SET @ver = "10.22.1";
SET @len = LENGTH(@ver);
SET @loc0 = 0;
SET @loc1 = LOCATE('.', @ver);
SET @loc2 = LOCATE('.', @ver, @loc1 + 1);
SET @loc3 = LOCATE('.', @ver, @loc2 + 1);
SELECT
-- 각각 자리별로 값 구하기.
-- SUBSTRING(@ver, (@loc0 + 1), IF(@loc1 <> 0, (@loc1 - @loc0 - 1), @len)) as 1st,
-- IF(@loc1 <> 0, SUBSTRING(@ver, (@loc1 + 1), IF(@loc2 <> 0, (@loc2 - @loc1 - 1), @len)), 0) as 2nd,
-- IF(@loc2 <> 0, SUBSTRING(@ver, (@loc2 + 1), IF(@loc3 <> 0, (@loc3 - @loc2 - 1), @len)), 0) as 3rd;
-- 각각 자리별로 값 구하기.(decimal type 으로 convert)
-- CONVERT(SUBSTRING(@ver, (@loc0 + 1), IF(@loc1 <> 0, (@loc1 - @loc0 - 1), @len)), DECIMAL) as 1st,
-- CONVERT(IF(@loc1 <> 0, SUBSTRING(@ver, (@loc1 + 1), IF(@loc2 <> 0, (@loc2 - @loc1 - 1), @len)), 0), DECIMAL) as 2nd,
-- CONVERT(IF(@loc2 <> 0, SUBSTRING(@ver, (@loc2 + 1), IF(@loc3 <> 0, (@loc3 - @loc2 - 1), @len)), 0), DECIMAL) as 3rd;
-- 실제 비교할 땐 어떻게 하지?? 각각 버전값이 0-99 까지 쓴다고 가정하고...
-- CONVERT(SUBSTRING(@ver, (@loc0 + 1), IF(@loc1 <> 0, (@loc1 - @loc0 - 1), @len)), DECIMAL) * 1000000 as 1st,
-- CONVERT(IF(@loc1 <> 0, SUBSTRING(@ver, (@loc1 + 1), IF(@loc2 <> 0, (@loc2 - @loc1 - 1), @len)), 0), DECIMAL) * 1000 as 2nd,
-- CONVERT(IF(@loc2 <> 0, SUBSTRING(@ver, (@loc2 + 1), IF(@loc3 <> 0, (@loc3 - @loc2 - 1), @len)), 0), DECIMAL) * 1 as 3rd;
-- 실제 비교할 땐 어떻게 하지?? 숫자값으로 변환해서 비교??
((CONVERT(SUBSTRING(@ver, (@loc0 + 1), IF(@loc1 <> 0, (@loc1 - @loc0 - 1), @len)), DECIMAL) * 1000000) +
(CONVERT(IF(@loc1 <> 0, SUBSTRING(@ver, (@loc1 + 1), IF(@loc2 <> 0, (@loc2 - @loc1 - 1), @len)), 0), DECIMAL) * 1000) +
(CONVERT(IF(@loc2 <> 0, SUBSTRING(@ver, (@loc2 + 1), IF(@loc3 <> 0, (@loc3 - @loc2 - 1), @len)), 0), DECIMAL) * 1)) as ver_sum;
실제 사용시에는 이렇게....
SELECT
SUBSTRING(appVer, 1, IF(LOCATE('.', appVer) <> 0, (LOCATE('.', appVer) - 1), LENGTH(appVer))) as 1st,
IF(LOCATE('.', appVer) <> 0, SUBSTRING(appVer, (LOCATE('.', appVer) + 1), IF(LOCATE('.', appVer, (LOCATE('.', appVer) + 1)) <> 0, (LOCATE('.', appVer, (LOCATE('.', appVer) + 1)) - LOCATE('.', appVer) - 1), LENGTH(appVer))), 0) as 2nd,
IF(LOCATE('.', appVer, (LOCATE('.', appVer) + 1)) <> 0, SUBSTRING(appVer, (LOCATE('.', appVer, (LOCATE('.', appVer) + 1)) + 1), IF(LOCATE('.', appVer, (LOCATE('.', appVer, (LOCATE('.', appVer) + 1)) + 1)) <> 0, (LOCATE('.', appVer, (LOCATE('.', appVer, (LOCATE('.', appVer) + 1)) + 1)) - LOCATE('.', appVer, (LOCATE('.', appVer) + 1)) - 1), LENGTH(appVer))), 0) as 3rd from tbl_app;
SELECT
((CONVERT(SUBSTRING(appVer, 1, IF(LOCATE('.', appVer) <> 0, (LOCATE('.', appVer) - 1), LENGTH(appVer))), DECIMAL) * 1000000) +
(CONVERT(IF(LOCATE('.', appVer) <> 0, SUBSTRING(appVer, (LOCATE('.', appVer) + 1), IF(LOCATE('.', appVer, (LOCATE('.', appVer) + 1)) <> 0, (LOCATE('.', appVer, (LOCATE('.', appVer) + 1)) - LOCATE('.', appVer) - 1), LENGTH(appVer))), 0), DECIMAL) * 1000) +
(CONVERT(IF(LOCATE('.', appVer, (LOCATE('.', appVer) + 1)) <> 0, SUBSTRING(appVer, (LOCATE('.', appVer, (LOCATE('.', appVer) + 1)) + 1), IF(LOCATE('.', appVer, (LOCATE('.', appVer, (LOCATE('.', appVer) + 1)) + 1)) <> 0, (LOCATE('.', appVer, (LOCATE('.', appVer, (LOCATE('.', appVer) + 1)) + 1)) - LOCATE('.', appVer, (LOCATE('.', appVer) + 1)) - 1), LENGTH(appVer))), 0), DECIMAL) * 1)) as ver_sum from tbl_app;