2

你能帮助我吗?

我的查询是

SELECT num FROM sortnum ORDER BY lpad(num, 10, 0) 

这个查询的结果不好 这里是结果

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
1a 
2a 
2b 
A1 
A2 
A3 
A4 
B1 
A10 
A11 
B10 

对于这种类型的数据,最好的查询是什么,我首先要编号,然后是以 a 开头的数据,然后是 b

我想要这样的结果

1 
1a 
2 
2a 
2b 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
A1 
A2 
A3 
A4 
A10 
A11 
B1 
B10 

提前致谢

4

2 回答 2

6

假设任何字母前缀的长度最多为一个字符,您可以这样做:

SELECT   num
FROM     sortnum
ORDER BY
  CAST(num AS UNSIGNED)=0,     -- those starting with numbers before non-numbers
  CAST(num AS UNSIGNED),       -- then by number prefix (if any)
  LEFT(num,1),                 -- then by first character
  CAST(MID(num,2) AS UNSIGNED) -- then by remaining numbers

sqlfiddle上查看。

于 2012-10-12T12:36:41.450 回答
0
SELECT num,
CONVERT(SUBSTRING_INDEX(num,'-',-1),UNSIGNED INTEGER) AS num
FROM sortnum
ORDER BY num;
于 2012-10-12T12:26:41.620 回答