id    time  a 
1            3 
2            2 
3            3 
4            4 
5            2 
6      
7            2 
我现在是按照时间排列 我想再加个条件 按照a排列 把a=3的放前面去  同时不要影响那些a=1,2位置 
结果如下 id    time   a 
1            3 
3             3 
2             2 
4            4 
5            2 
6      
7            2 说白了   
select * form table where a=3 order by time desc 
和  
select * form table where a!=3 order by time desc 2条语句的组合 改怎么写? 我数据库记录有大约30w了

解决方案 »

  1.   

    select id,time,a
    from table
    order by if(a=3,0,1),time desc select id,time,a
    from table
    order by case a when 3 then 0 else 1 end,time desc select id,time,a,0 as sno
    from table 
    where a=3
    union all
    select id,time,a,1 as sno
    from table 
    where a!=3
    order by sno,time desc这三条都行,但效率或许会有差别。自己试一下吧。
      

  2.   


    select * 
    from tb
    order by (case a when 3 then now()+999999 else time end) desc