问:

我有一个查询命令column

select * from mytable order by column asc — sort table

column类型是varchar,所以输出是:

1
10
100
11
12
13

如果我想让它们按数值排序,我应该如何排序,所以输出是:

1
10
11
12
13

 

答案

1.

使用:

order by cast(column as unsigned) asc

2.

column如果您只想将其视为,则可以使用它INT

SELECT * FROM mytable ORDER BY column+0;
1
10
11
12
13
100

或者如果你想column同时对待INTVARCHAR

SELECT * FROM mytable ORDER BY column+0, column; #this will sort the column by VARCHAR first and then sort it by INT

abc
xyz
1
10
11
12
13
100

Mysql Varchar 到数字转换以进行排序
标签: