包含数字的 SQL 顺序字符串 [英] SQL-Order Strings containing numbers
本文介绍了包含数字的 SQL 顺序字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的表中有以下值:
a 3
a 5
a 7
aa 5
a 10
b 5
使用 ORDER BY 命令,我得到以下信息:
With the ORDER BY command I get the following:
a 10
a 3
a 5
a 7
aa 5
b 5
我想要以下结果:
a 3
a 5
a 7
aa 5
a 10
b 5
任何想法如何在我的 SQL 查询中解决它?
Any ideas how I can solve it in my SQL query?
推荐答案
排序时可以使用substring_index()
来分割数据:
You can use substring_index()
to split data when order:
SELECT
col1
FROM
table1 t
ORDER BY
SUBSTRING_INDEX(col1, ' ', 1),
CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(col1, ' ', 2),' ',- 1) AS UNSIGNED)
但是 aa
终究会变成 a
因为您想要的排序逻辑与两个列"中的数据值相矛盾,其中之一必须是领先的.此查询返回:
However aa
will become after all a
as your desired ordering logic is contradicting with data values in both 'columns', one of them must be leading. This query returns:
a 3
a 5
a 7
a 10
aa 5
b 5
这篇关于包含数字的 SQL 顺序字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文