MySQL将order varchar转换为unsigned并排序" 0"到最后

时间:2015-10-19 15:42:42

标签: mysql sorting casting

在MySQL中,我需要ORDER BY CAST(varchar AS unsigned),但是,我还需要命令" 0"最后。我有可能做到这一点吗? 例如:

"1"
"10"
"0"
"2"

结果应该是

"1"
"2"
"10"
"0"

1 个答案:

答案 0 :(得分:1)

ORDER BY col = '0', CAST(col AS unsigned)

col = '0'如果不是0,则为1,如果是<div>,则它将首先排序所有非零值。然后它会在这两组中按数字排序。