以下数据是按升序排序的字符串列:
1/44/104/66
1/44/104/66/137
1/44/104/66/138
1/44/104/66/139
1/44/104/66/140
1/44/104/66/141
1/44/104/66/142
1/44/104/66/143
1/44/104/66/67
1/44/104/66/68
1/44/104/66/69
但我想这样排序:
1/44/104/66
1/44/104/66/67
1/44/104/66/68
1/44/104/66/69
1/44/104/66/137
1/44/104/66/138
1/44/104/66/139
1/44/104/66/140
1/44/104/66/141
1/44/104/66/142
1/44/104/66/143
答案 0 :(得分:0)
对于您拥有的数据,这应该有效:
order by length(col), col
但是,这假设唯一的区别在于最后一列。
答案 1 :(得分:0)
您可以尝试以下方式:
select
Name
from TableOne
ORDER BY Cast(Replace(Name, "/", "") as UNSIGNED)
答案 2 :(得分:0)
SELECT * FROM myTable
ORDER BY CAST(SUBSTRING_INDEX(DATA_COLUMN,"/",-1) AS UNSIGNED) ;
工作原理:
1:在您的原始订单中,订单不正确,因为它是字符串比较字符串,而您希望基于整数值进行排序。
2 ..在此解决方案中,SUBSTRING_INDEX
提取/
之后的最后一个值,然后将其转换为整数以提供所需的顺序。
3 ..您可以通过数据模式中的倒数第二个,最后一个数字扩展相同的解决方案。
答案 3 :(得分:0)
不是一个漂亮的查询,但它应该做你需要的:
SELECT s
FROM
yourtable
ORDER BY
s + 0,
CASE WHEN LENGTH(s)-LENGTH(REPLACE(s, '/',''))>0
THEN SUBSTRING_INDEX(SUBSTRING_INDEX(s, '/', 2), '/', -1)+0
ELSE 0 END,
CASE WHEN LENGTH(s)-LENGTH(REPLACE(s, '/',''))>1
THEN SUBSTRING_INDEX(SUBSTRING_INDEX(s, '/', 3), '/', -1)+0
ELSE 0 END,
CASE WHEN LENGTH(s)-LENGTH(REPLACE(s, '/',''))>2
THEN SUBSTRING_INDEX(SUBSTRING_INDEX(s, '/', 4), '/', -1)+0
ELSE 0 END,
CASE WHEN LENGTH(s)-LENGTH(REPLACE(s, '/',''))>3
THEN SUBSTRING_INDEX(SUBSTRING_INDEX(s, '/', 5), '/', -1)+0
ELSE 0 END
请参阅小提琴here。