SQL按字符串+整数的顺序

时间:2018-08-06 11:16:29

标签: mysql sql substring

如何按字符串末尾的ID排序?

例如,我想按末尾的数字对这些字符串进行排序(在RSPP之后):

RSPP891
RSPP896
RSPP897
RSPP898
RSPP899
RSPP900
RSPP901
RSPP902
RSPP903
RSPP904
RSPP730
RSPP731
RSPP1380
RSPP733
RSPP734

我读了一些有关子字符串的知识,所以我尝试了该查询,但没有成功。

SELECT `RsppTags` FROM Table WHERE ConnectionID = 15202  
ORDER BY
SUBSTR(RsppTags FROM 4 FOR LENGTH(RsppTags)-1),
CAST(SUBSTR(RsppTags FROM 1) AS UNSIGNED)

是否可以获取此数据ORDER BY ASC

5 个答案:

答案 0 :(得分:2)

SELECT RsppTags FROM Table WHERE ConnectionID = 15202 ORDER BY CAST(SUBSTR(RsppTags FROM 5) AS UNSIGNED)

默认情况下,MYSQL提供ASC排序。

尝试上述查询。 它将为您提供帮助。

答案 1 :(得分:1)

尝试:

ORDER BY CAST(SUBSTR(RsppTags FROM 5) AS UNSIGNED) ASC

Sample SQL Fiddle showing the result

答案 2 :(得分:1)

order by SUBSTR(RsppTags, 5) + 0

答案 3 :(得分:1)

尝试

SELECT `RsppTags` FROM Table WHERE ConnectionID = 15202  
ORDER BY CAST(SUBSTR(RsppTags, 5) AS UNSIGNED) ASC;

答案 4 :(得分:1)

SELECT `RsppTags` 
FROM Table WHERE ConnectionID = 15202  
ORDER BY
SUBSTR(RsppTags,1,4), 
CAST(SUBSTR(RsppTags,5) AS UNSIGNED);