mysql在排序时将包含字母的字段视为最低值

mysql在排序时将包含字母的字段视为最低值,mysql,sql,Mysql,Sql,我有一个表,它的(x)列包含两个数字和字母。当我使用 ORDER BY x DESC 它将包含字母的字段置于顶部。如何使其将包含字母的字段视为最低值?您可以在order by中使用case语句: 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

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

ORDER BY x DESC 

它将包含字母的字段置于顶部。如何使其将包含字母的字段视为最低值?

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

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 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;

按x+0描述排序-应该可以

另一个可能的选项是按左('0'+x,2)描述排序。这将确保
0-9
为零填充,从而使字符值
01
小于
10

select * from Table1 order by 
CAST(x AS UNSIGNED),x;

我不这么认为。问题是关于包含字母的值。此答案处理以非数字字符开头的值。@DanBracuk。如果没有样本数据,这个问题是不明确的。我修改了我的答案。我假设MySQL支持您的修改中的语法。我真的不知道。我已经取消了我的反对票。