I have values like:
1.1.2
9.1
2.2
4
1.2.3.4
3.2.14
3.2.1.4.2
.....
I need to sort those values using mysql. The data type for this one is
Try abusing the INET_ATON function to do the sorting like so:
SELECT version_number FROM table ORDER BY INET_ATON(SUBSTRING_INDEX(CONCAT(version_number,'.0.0.0'),'.',4))
This trick was originally posted on the mysql mailing list, so many thanks to the original poster, Michael Stassen!
Here's what he had to say:
If each part is no larger than 255, you can leverage INET_ATON() to do what you want (up to the 4th part). The trick is making each of these look like an IP first by using CONCAT to add '0.0.0' to make sure every row has at least 4 parts, then SUBSTRING_INDEX to pull out just the first 4 parts.
Now, I must point out that because we are sorting on a function of the column, rather than on the column itself, we cannot use an index on the column to help with the sort. In other words, the sorting will be relatively slow.
In the latter case, he recommends a solution similar to the one posted by @spanky (separate columns).