具有编号的SQL ORDER BY字符串
问题描述:
我想要按标题开头的oder,它以数字开头,并且是数字和字符串的组合。具有编号的SQL ORDER BY字符串
示例数据:
title
------------
1 Blub
2 Blub
3 Blub
4 Blub
5 Blub
6 Blub
7 Blub
8 Blub
9 Blub
10 Blub
11 Blub
12 Blub
13 Blub
默认ORDER BY title
给我出来:
1 Blub
10 Blub
11 Blub
12 Blub
13 Blub
2 Blub
20 Blub
21 Blub
是否有可能获得通过数值正确的ORDER BY
?
1 Blub
2 Blub
10 Blub
11 Blub
12 Blub
13 Blub
20 Blub
21 Blub
答
ORDER BY CONVERT(INT, SUBSTRING(title, 1, 2))
应该工作,因为空间将被忽略。如果您的数字高于99,则不起作用。
答
试试这个:
order by cast(LEFT(title,LOCATE(' ',title) - 1) as unsigned)
答
这将你的领域分割成数和文字部分,将责令其则。
order by CONVERT(INT, SUBSTRING_INDEX(title, ' ', 1)) ASC, SUBSTRING_INDEX(title, ' ', -1) ASC
'order by title + 0' should do it。 –