mysql对版本号的排序 [英] mysql sorting of version numbers

查看:1161
本文介绍了mysql对版本号的排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的值像:

1.1.2 
9.1 
2.2
4
1.2.3.4
3.2.14
3.2.1.4.2
.....

我需要使用mysql对这些值进行排序.该数据类型为varbinary(300).

I need to sort those values using mysql. The data type for this one is varbinary(300).

所需的输出如下:

1.1.2
1.2.3.4
2.2
3.2.1.4.2
3.2.14
4
9.1

查询是:

select version_number from table order by version_number asc 

它没有给出正确的排序顺序.

it does not give the correct sorting order.

所需的输出是:

1.1.2
1.2.3.4
2.2
3.2.1.4.2
3.2.14 
4
9.1

版本号最多20位(例如1.2.3.4.5.6.7.8.9.2.34),甚至更多.没有特定的最大大小,标准版本与上面提到的一样.

The version numbers are up to 20 digits (like 1.2.3.4.5.6.7.8.9.2.34) and more also. There is no particular max size and the standard version is just like above mentioned.

推荐答案

尝试滥用INET_ATON函数进行排序,如下所示:

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))

此技巧最初发布在 mysql邮件列表上,非常感谢原始海报Michael Stassen!

This trick was originally posted on the mysql mailing list, so many thanks to the original poster, Michael Stassen!

这是他必须说的:

如果每个部分不大于255,则可以利用INET_ATON()来执行 您想要什么(直到第四部分).诀窍在于使每一个 首先使用CONCAT添加"0.0.0"以确保每个 该行至少包含4个部分,然后用SUBSTRING_INDEX拉出仅 前4个部分.

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.

在后一种情况下,他建议使用类似于@spanky(单独的列)发布的解决方案.

In the latter case, he recommends a solution similar to the one posted by @spanky (separate columns).

这篇关于mysql对版本号的排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆