Je zou ze kunnen opsplitsen in hun samenstellende delen, zoals:
SELECT REPLACE(SUBSTRING(SUBSTRING_INDEX(prog_id, '.', 1),
LENGTH(SUBSTRING_INDEX(prog_id, '.', 1 -1)) + 1),
'.', '') AS id1,
REPLACE(SUBSTRING(SUBSTRING_INDEX(prog_id, '.', 2),
LENGTH(SUBSTRING_INDEX(prog_id, '.', 2 -1)) + 1),
'.', '') AS id2,
REPLACE(SUBSTRING(SUBSTRING_INDEX(prog_id, '.', 3),
LENGTH(SUBSTRING_INDEX(prog_id, '.', 3 -1)) + 1),
'.', '') AS id3
FROM programs
ORDER BY CAST(id1 AS INT(4)), CAST(id2 AS INT(4)), CAST(id3 AS INT(4))
De beste methode zou zijn om de extra velden te maken zoals yoda2k zegt, maar als je die toegang niet hebt, kun je het bovenstaande gebruiken.
Je zou dat kunnen inkapselen in een functie als:
CREATE FUNCTION SPLIT_STR(
x VARCHAR(255),
delim VARCHAR(12),
pos INT
)
RETURNS VARCHAR(255)
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
delim, '');
Doe dan:
SELECT SPLIT_STR(prog_id, '.', 1) AS id1,
SPLIT_STR(prog_id, '.', 2) AS id2,
SPLIT_STR(prog_id, '.', 3) AS id3,
FROM programs
ORDER BY CAST(id1 AS INT(4)), CAST(id2 AS INT(4)), CAST(id3 AS INT(4))