表zone:
id name pid
1 黑龙江省 0
2 辽宁省 0
4 哈尔滨 1
5 北京 0
6 沈阳 2
7 长沙 8
8 湖南 0这是一个省市表,pid字段记录市的上级省
我想通过一个查询把他们关联起来就像下面这样:
黑龙江省
哈尔滨
辽宁省
沈阳
湖南
长沙
北京 如何查询?
id name pid
1 黑龙江省 0
2 辽宁省 0
4 哈尔滨 1
5 北京 0
6 沈阳 2
7 长沙 8
8 湖南 0这是一个省市表,pid字段记录市的上级省
我想通过一个查询把他们关联起来就像下面这样:
黑龙江省
哈尔滨
辽宁省
沈阳
湖南
长沙
北京 如何查询?
解决方案 »
- ajax updatepanel
- 关于SQL防注入的问题
- 页面不能提交数据的解决方案
- 简单的问题,很着急啊!解决者,当场给分!
- 为什么把.net程序上传到服务器上button不能用了?很急很急的
- Unable to find an entry point named 高手帮忙,调用dll出错
- web实现点对点的通信
- 散掉最后可用分,只求解脱...
- 用微软的treeview控件能实现CSDN左边树形上边的movenext,moveprevious,还有同步的功能吗?能否给个例子?
- Thanks for your books, spring_ok. Please come in:)
- 如何用html和css控制一个table中的每个td的样式
- 如何从水晶报表中取得运算完的结果?
Create Table zone
(id Int,
name Nvarchar(10),
pid Int)
--插入數據
Insert zone Select 1, N'黑龙江省', 0
Union All Select 2, N'辽宁省', 0
Union All Select 4, N'哈尔滨', 1
Union All Select 5, N'北京', 0
Union All Select 6, N'沈阳', 2
Union All Select 7, N'长沙', 8
Union All Select 8, N'湖南', 0
GO
--建立存儲過程
Create Procedure SP_TEST
As
Begin
--深度排序显示处理
--生成每个节点的编码累计
Declare @Level Int
Set @Level=0
Select id, id As pid, @Level As Level,',' + Rtrim(id) As Sort Into #T
From zone
Where pid = 0 While @@ROWCOUNT>0
Begin
Set @Level = @Level + 1
Insert #T Select A.id, B.pid, @Level, B.Sort+',' + Rtrim(A.id)
From zone A, #T B
Where A.pid = B.id
And B.Level = @Level - 1
End --显示结果
Select SPACE(B.Level *2) + Rtrim(A.Name) As Name
From zone A, #T B
Where A.id = B.id
Order By B.pid, B.Sort --刪除臨時表
Drop Table #T
End
GO
--測試
EXEC SP_TEST
GO
--刪除測試環境
Drop Table zone
Drop Procedure SP_TEST
--結果
/*
Name
黑龙江省
哈尔滨
辽宁省
沈阳
北京
湖南
长沙
*/
(id Int,
name Nvarchar(10),
pid Int)
--插入數據
Insert zone Select 1, N'黑龙江省', 0
Union All Select 2, N'辽宁省', 0
Union All Select 4, N'哈尔滨', 1
Union All Select 5, N'北京', 0
Union All Select 6, N'沈阳', 2
Union All Select 7, N'长沙', 8
Union All Select 8, N'湖南', 0select * into Test from zone
declare @cityNum int
declare @cityName varchar (20)
declare @cName varchar(20)
declare @pNum int
declare @cNum int
select @cityNum=count(pid) from test where pid=0 print @cityNumwhile @cityNum>0
begin
select @cityName=name from test where pid=0
print @cityName
select @pNum=
case
when @cityName='湖南' then 8
when @cityName='黑龙江省' then 1
when @cityName='辽宁省' then 2
else 100
end
select @cNum=count(pid) from test where pid=@pNum
while @cNum>0
begin select @cName=name from test where pid=@pNum
print @cName
delete from test where name=@cName
select @cNum=@cNum-1
end
delete from test where name=@cityName
select @cityNum=@cityNum-1
end
id name pid
1 黑龙江省 0
2 辽宁省 0
4 哈尔滨 1
5 北京 0
6 沈阳 2
7 长沙 8
8 湖南 0select *, id as o1,'a' as type from zone where pid=0
union
select *, pid as o1,'b' as type from zone where pid in ( select id from zone )
order by o1,type,id;
(Case When B.Name Is Not Null Then ' ' Else '' End) + A.Name As Name
From
zone A
Left Join
zone B
On A.pid = B.id
Order By IsNull(B.id, A.id), A.pid, A.id
--建立測試環境
Create Table zone
(id Int,
name Nvarchar(10),
pid Int)
--插入數據
Insert zone Select 1, N'黑龙江省', 0
Union All Select 2, N'辽宁省', 0
Union All Select 4, N'哈尔滨', 1
Union All Select 5, N'北京', 0
Union All Select 6, N'沈阳', 2
Union All Select 7, N'长沙', 8
Union All Select 8, N'湖南', 0
GO
Select
(Case When B.Name Is Not Null Then ' ' Else '' End) + A.Name As Name
From
zone A
Left Join
zone B
On A.pid = B.id
Order By IsNull(B.id, A.id), A.pid, A.id
GO
--刪除測試環境
Drop Table zone
--結果
/*
Name
黑龙江省
哈尔滨
辽宁省
沈阳
北京
湖南
长沙
*/
呵呵。顶了```
ind = case pid
when 0 then id
else select top 1 b.id from zone a join zone b on a.pid = b.id where a.pid = c.pid
*
from zone c) d
order by d.ind