例如表t1
id name type
1  xxxx 1
2  xxxs 1
3  xxxq 2
select top 5 * from t1 where type = 1
比如要取type=1的5条记录,但是如果type=1的不足5条,比如只有2条,那就去掉type=1的条件再取3条,也就是总记录数总是5条

解决方案 »

  1.   

    select top 5 * from t1 order by type asc;
      

  2.   

    select top 5 * from t1 order by case when type = 1 then 0 else 1 end asc,type;
      

  3.   

    select
     top 5 * 
    from 
     t1 
    order by 
     case when type = 1 then 0 else 1 end asc,type
      

  4.   


    --嗯:写的好,也许type有小于1的!考虑得周到!
      

  5.   

    select top 5 * from t1 order by [type] asc;
      

  6.   

    select top 5* 
    from table
    order by (case type when '1'then 1 else 0 end) desc