按某一字段排序,先按字母升序再按数字降序排
例如
字段
ab1
ab2
ab11
bc2
bc12
cd2
排序之后应为
字段
ab11
ab2
ab1
bc12
bc2
cd2
HQL语句应该怎么写,求高手解答?

解决方案 »

  1.   


    --确定的话如下:
    --> 测试数据: [tb]
    if object_id('[tb]') is not null drop table [tb]
    create table [tb] (字段 varchar(4))
    insert into [tb]
    select 'ab1' union all
    select 'ab2' union all
    select 'ab11' union all
    select 'bc2' union all
    select 'bc12' union all
    select 'cd2'--开始查询
    select * from [tb] order by LEFT(字段,2),CONVERT(int,RIGHT(字段,len(字段)-2)) desc--结束查询
    drop table [tb]/*
    字段
    ----
    ab11
    ab2
    ab1
    bc12
    bc2
    cd2(6 行受影响)
      

  2.   

    如果是sql server:select * from tb order by left(col,2) , cast(substring(col,3,len(col)) as int) desc
      

  3.   

    use Tempdb
    go
    --> --> 
     
    if not object_id(N'Tempdb..#T') is null
    drop table #T
    Go
    Create table #T([字段] nvarchar(10))
    Insert #T
    select N'ab1' union all
    select N'ab2' union all
    select N'ab11' union all
    select N'bc2' union all
    select N'bc12' union all
    select N'cd2'
    Go
    Select * 
    from #T
    ORDER BY LEFT([字段],PATINDEX('%[0-9]%',[字段])-1),CAST(RIGHT([字段],PATINDEX('%[^0-9]%',REVERSE([字段]))-1) AS INT)
    /*
    ab1
    ab2
    ab11
    bc2
    bc12
    cd2
    */
      

  4.   


    --> 测试数据: [tb]
    if object_id('[tb]') is not null drop table [tb]
    create table [tb] (字段 varchar(4))
    insert into [tb]
    select 'ab1' union all
    select 'ab2' union all
    select 'ab11' union all
    select 'bc2' union all
    select 'bc12' union all
    select 'cd2'--开始查询
    select * from tb 
    order by left(字段,patindex('%[0-9]%',字段)-1),
    convert(int,substring(字段,patindex('%[0-9]%',字段),len(字段))) desc--结束查询
    drop table [tb]/*
    字段
    ----
    ab11
    ab2
    ab1
    bc12
    bc2
    cd2(6 行受影响)
      

  5.   

    思路,先找到第一个数字的位置,然后减1就是字母的最后一位,然后用left取出字母,升序
    再从第一个数字的位置一直取到最后一位,就是数字的位置,转为数字型,倒序
    希望LZ看得懂
      

  6.   

    语言都是相通的,主要是看思路,思路告诉你了
    具体的你根据hql语法自己改吧,hql我是不会。