转自:http://www.maomao365.com/?p=7141
摘要:
下文讲述sql脚本编写中,将 特定值排在最前面的方法分享,
实验环境:sqlserver 2008 r2
例:将数据表中指定值为0的行排在最前面呈现给用户
create table test(keyid int identity,info varchar(10),flag int) go insert into test(info,flag)values ('a',-100),('b',-2),('c',-3) ,('d',2),('e',4),('f',8),('g',9),('h',0),('e',1),('f',0) go ---将flag值等于0的放入最前面显示 select * from test order by case when flag =0 then 0 else 1 end , flag asc go ---将flag值等于2的放入最前面显示 select * from test order by case when flag =2 then 0 else 1 end , flag asc go go truncate table test drop table test