轉自:http://www.maomao365.com/?p=7141sql
摘要:
下文講述sql腳本編寫中,將 特定值排在最前面的方法分享,
實驗環境:sqlserver 2008 R2ide
例:將數據表中指定值爲0的行排在最前面呈現給用戶sqlserver
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