首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

这个怎么排序

2012-01-18 
这个如何排序?字段一,字段二A05,A06A06,A07A07,--A10,A11A11,A12A12,A05 sort A10,A11A11,A12A12,A05

这个如何排序?
字段一,字段二
A05,A06
A06,A07
A07,--
A10,A11
A11,A12
A12,A05

>>> sort >>
A10,A11
A11,A12
A12,A05
A05,A06
A06,A07
A07,--

请教了!

[解决办法]
什么规律?
[解决办法]
只能做到这一步....

SQL code
-->生成测试数据 declare @tb table([字段一] nvarchar(3),[字段二] nvarchar(3))Insert @tbselect N'A05',N'A06' union allselect N'A06',N'A07' union allselect N'A07',N'--' union allselect N'A10',N'A11' union allselect N'A11',N'A12' union allselect N'A12',N'A05'Select * from @tb order bycase when [字段二] not in (select [字段一] from @tb) then [字段一] else [字段二] end/*字段一  字段二---- ----A12  A05A05  A06A06  A07A07  --A10  A11A11  A12*/
[解决办法]
SQL code
create table tb(a varchar(50),b varchar(50))goinsert into tb select 'A05','A06'insert into tb select 'A06','A07'insert into tb select 'A07','--'insert into tb select 'A10','A11'insert into tb select 'A11','A12'insert into tb select 'A12','A05'godeclare @root varchar(50)select @root=a from tb a where not exists(select 1 from tb where b=a.a);with depth as(    select * from tb where a=@root    union all    select a.* from tb a,depth b        where a.a=b.b)select * from depthgodrop table tbgo 

热点排行