ID  COL
1    A
2    B
3    C
4    D
5    A
6    B
7    C
8    D传递条件 COL=‘A’ 则
ID  COL
1    A
5    A
2    B
6    B
3    C
7    C
4    D
8    D
传递条件 COL=‘B’ 则
ID  COL
2    B
6    B
1    A
5    A
3    C
7    C
4    D
8    D也就是输入条件的排在前面 后面分组

解决方案 »

  1.   


    select * from tb order by case when col='b' then 0 else 1 end,col
    b在前面,其他还按字母派
      

  2.   

    select ID,COL
    from tablea
    order by case when col = 'A' then 0 else 1 end
      

  3.   


    create table [tablea] (id int, col char)
    insert into [tablea] 
    select 1,'A' union
    select 2,'B' union
    select 3,'C' union
    select 4,'D' union
    select 5,'A' union
    select 6,'B' union
    select 7,'C' union
    select 8,'D'declare @col char set @col = 'B'
    select ID, COL from [tablea]
    order by case when col=@col then '' else col end,ID
      

  4.   

    wzy_love_sly 啥原理能不能讲讲
      

  5.   

    按照表达式"case when col=@col then '' else col end"的值排序
    当col=@col时表达式取值为''(空串),小于任何字符,故而排在前面
      

  6.   

    SELECT id, col
    FROM  T
    ORDER BY CASE WHEN col = @colValue THEN '' ELSE col END;