sql按字符串+整数排序

but5z9lq  于 2021-06-20  发布在  Mysql
关注(0)|答案(5)|浏览(389)

如何按字符串末尾有id的字符串排序?
例如,我想按末尾的数字(rspp之后)对这些字符串进行排序:

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

我读了一些关于substring的内容,所以我尝试了这个查询,但是没有成功。

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 ?

7cjasjjr

7cjasjjr1#

order by SUBSTR(RsppTags, 5) + 0
qcbq4gxm

qcbq4gxm2#

尝试:

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

显示结果的示例sql fiddle

s4n0splo

s4n0splo3#

SELECT RsppTags FROM Table WHERE ConnectionID = 15202 ORDER BY CAST(SUBSTR(RsppTags FROM 5) AS UNSIGNED) 默认情况下,mysql提供asc排序。
尝试以上查询。它会帮助你的。

hgncfbus

hgncfbus4#

尝试

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

ifmq2ha25#

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

相关问题