4

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

 1 year ago
source link: https://blog.p2hp.com/archives/10270
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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

我有一个查询命令column

select * from mytable order by column asc — sort table

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

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



order by cast(column as unsigned) asc

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

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK