2

我有一个包含两个数字字母的列 (x) 的表。当我使用

ORDER BY x DESC 

它将包含字母的字段放在顶部。如何使它将包含字母的字段视为最小值?

4

4 回答 4

5

您可以在以下语句中使用 case 语句order by

order by (case when left(x, 1) between '0' and '9' then 0 else 1 end),
         x desc

编辑:

如果您想要在顶部包含字母的字段

order by (case when x like '%[a-zA-Z]%' then 0 else 1 end),
         x desc;

如果您想要顶部包含字母的字段:

order by (case when x not like '%[^a-zA-Z]%' then 0 else 1 end),
         x desc;

如果您想要底部仅包含数字的字段:

order by (case when x not like '%[^0-9]%' then 1 else 0 end),
         x desc;
于 2013-07-26T11:53:41.733 回答
0

ORDER BY x+0 DESC - 应该可以

于 2013-07-26T12:10:27.873 回答
0

另一种可能的选择是ORDER BY LEFT('0' + x, 2) DESC. 这将确保0 - 9被零填充,从而使字符值01小于10

于 2013-07-26T12:15:00.350 回答
0
select * from Table1 order by 
CAST(x AS UNSIGNED),x;

小提琴

于 2013-07-26T13:35:01.157 回答