我有一个包含两个数字字母的列 (x) 的表。当我使用
ORDER BY x DESC
它将包含字母的字段放在顶部。如何使它将包含字母的字段视为最小值?
您可以在以下语句中使用 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;
ORDER BY x+0 DESC - 应该可以
另一种可能的选择是ORDER BY LEFT('0' + x, 2) DESC
. 这将确保0 - 9
被零填充,从而使字符值01
小于10
。
select * from Table1 order by
CAST(x AS UNSIGNED),x;