我有个字段是varchar,里面有的是数字,我想从里面找出小于 一个变量数字 的记录,例如所有小于200的记录,然后按照这个字段降序,
我见有的说用CAST,但是我在后面排序试了一下:order by CAST(o.value as int) desc
不行,下面是我写的sql,order前面都正确.就是排序不正确,要是啥都不加,他会按value的首字母进行排序,那样,80都排到100前面了SELECT * FROM out as o where (o.group = 'config') and (o.key like '%config_discount%') and (o.value <=120) and o.value >0 order by CAST(o.value as int) desc; 

解决方案 »

  1.   


    SELECT * FROM out as o where (o.group = 'config') and (o.key like '%config_discount%') and (o.value <=120) and o.value >0 order by CAST(o.value as unsigned) desc; 
      

  2.   

    估计你的这个 '我有个字段是varchar,里面有的是数字' 是 o.value SELECT * 
    FROM out as o 
    where (o.group = 'config') 
    and (o.key like '%config_discount%') 
    and (o.value  <=120) 
    and o.value >0 
    order by o.value +0  desc; 不知道有没有猜中。问题说明越详细,回答也会越准确!参见如何提问。(提问的智慧
      

  3.   

    非常感谢ACMAIN_CHM 的解答,order by o.value +0  desc 起作用了