日期:2014-05-17 浏览次数:20512 次
create table #tb( id int, tid int)
insert into #tb
select 1,NULL
union all select 2,NULL
union all select 3,NULL
union all select 4,NULL
union all select 5,NULL
union all select 6,2
union all select 7,2
union all select 8,1
union all select 9,3
select *
from
(select distinct a.*
from #tb a
left join #tb b on a.id=b.tid
)a
order by case when a.tid IS null then a.id else a.tid end,a.tid
drop table #tb
/*
id tid
1 NULL
8 1
2 NULL
6 2
7 2
3 NULL
9 3
4 NULL
5 NULL
*/