SQL:比较MySQL中的版本字符串(W.X.Y.Z) [英] SQL: Compare Version string (W.X.Y.Z) in MySQL

查看:439
本文介绍了SQL:比较MySQL中的版本字符串(W.X.Y.Z)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题类似于这个问题,除了我有4个群组,而不是3个.

My problem is similar to this one except I have 4 groups, not 3.

在MySQL中,是否只有一种方法可以按版本字符串对具有4个组的字符串进行排序?

Is there a way to order by version strings with 4 groups in MySQL only?

其中一些示例版本为:

10.0.0.0
11.0.0.3
12.0.0.125
13.8.6.800

总是只有4个组.

推荐答案

Uri关于使用UDF的评论可能是最好的.但是,如果您做不到,那么可以使用另一种方法:它利用* 1和

Uri's comment on using a UDF is likely best. However, if you can't here's a different approach: it takes advantage of implicit casting using *1 and substring_index function which "Return a substring from a string before the specified number of occurrences of the delimiter"

SELECT Version
FROM foo
order by 
SUBSTRING_INDEX( `version` , '.', 1 )*1,
SUBSTRING_INDEX(SUBSTRING_INDEX( `version` , '.', 2 ),'.',-1)*1, 
SUBSTRING_INDEX(SUBSTRING_INDEX( `version` , '.', -2 ),'.',1)*1,
SUBSTRING_INDEX( `version` , '.', -1 )*1

SQL提琴

另外,.之间的值可以是任意长度,只要它们是数字数据类型即可.

additionally the values between the .'s can be of any length just as long as they are of numeric data type.

来源: http://dev .mysql.com/doc/refman/5.6/en/string-functions.html#function_substring-index 在2006年3月22日晚上8:02的按名称保留的用户的注释中

source found: http://dev.mysql.com/doc/refman/5.6/en/string-functions.html#function_substring-index in comments by name withheld user on March 22 2006 8:02pm

这篇关于SQL:比较MySQL中的版本字符串(W.X.Y.Z)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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