SQL里面用order by排序为什么必须01,02这样排?
如果1,2,3....10,11就会出现1,10,11,2,3,4?

解决方案 »

  1.   

    order by cast(字段名 as int)
      

  2.   


    declare @t  table (t varchar(2))
    insert into @t
    select '1' union all 
    select '11' union all 
    select '12' union all 
    select '3' union all 
    select '4' select * from @t 
    order by cast (t as int )
      

  3.   

    order by 中的默认排序是按字符顺序的
    除非你转换成数字支持YiZhiNet(我姓义很多人都觉得奇怪)的
    order by cast(字段 as int)
      

  4.   

    进一步想问问,如果是有字母和数字的组合排序,应该如何?如,abcdef1,abcdef2,abcdef10,这里Order就只能是abcdef1,abcdef10,abcdef2了,而且这种也不能转换
      

  5.   

    -------可以把数字提取出来,然后排序
    declare @t table(a varchar(20))
    insert into @t select 'abcdef1' 
    union all select 'abcdef2' 
    union all select 'abcdef10'
    union all select 'abcdef90'
    union all select 'abcdef1040'select * from @t order by cast(substring(a,patindex('%[0-9]%',a),len(a)) as int)