转载:SQL 递归树 子父节点相互查询
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([modeid] int,modename varchar(20),parentid int)
insert [tb]
select 100 ,'商品管理', 0 union all
select 101 ,'定单管理', 0 union all
select 102 ,'用户管理', 0 union all
select 104 ,'学院广告', 0 union all
select 105 ,'系统设置', 0 union all
select 106 ,'附件管理', 0 union all
select 107 ,'商品管理', 100 union all
select 108 ,'明细管理', 100 union all
select 109 ,'物流管理', 100 union all
select 110 ,'商品信息管理', 107 union all
select 111 ,'商品分类管理', 107 union all
select 112 ,'回收站管理', 107 union all
select 114 ,'团购管理', 108 union all
select 115 ,'拍卖管理', 108 union all
select 116 ,'优惠管理', 108 union all
select 117 ,'会员管理', 102 union all
select 118 ,'会员卡管理', 102 union all
select 119 ,'资金管理', 102 union all
select 120 ,'管理员管理', 102 union all
select 121 ,'添加管理员', 120 union all
select 122 ,'修改管理员', 120
go
--查所有子结点
if object_id('f_getC') is not null drop function f_getC
go
create function f_getC(@id int)
returns @re table(id int,level int,sort varchar(10))
as
begin
declare @l int
set @l=0
insert @re select @id,@l,null
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.modeid,@l,ltrim(isnull(b.sort,a.modeid)) from tb as a,@re as b
where b.id=a.parentid and b.level=@l-1
end
update @re set level = level -1
return
end
go
select a.modeid,a.parentid,REPLICATE(' ',b.level) +'┝'+a.modename,b.level,b.sort from tb a,f_getC(0) b
where a.modeid=b.id
order by case when b.level<2 then 0 else 1 end,b.sort,b.level
/*
modeid parentid sort level
----------- ----------- -------------------------------------------------- ---------- -----------
100 0 ┝商品管理 100 0
107 100 ┝商品管理 100 1
108 100 ┝明细管理 100 1
109 100 ┝物流管理 100 1
101 0 ┝定单管理 101 0
102 0 ┝用户管理 102 0
117 102 ┝会员管理 102 1
118 102 ┝会员卡管理 102 1
119 102 ┝资金管理 102 1
120 102 ┝管理员管理 102 1
104 0 ┝学院广告 104 0
105 0 ┝系统设置 105 0
106 0 ┝附件管理 106 0
110 107 ┝商品信息管理 100 2
111 107 ┝商品分类管理 100 2
112 107 ┝回收站管理 100 2
114 108 ┝团购管理 100 2
115 108 ┝拍卖管理 100 2
116 108 ┝优惠管理 100 2
121 120 ┝添加管理员 102 2
122 120 ┝修改管理员 102 2
(所影响的行数为 21 行)
*/
--查所有子结点,带路径与排序
if object_id('f_getC') is not null drop function f_getC
go
create function f_getC(@id int)
returns @re table(id int,level int,sort varchar(100),path varchar(500))
as
begin
declare @l int
set @l=0
insert @re
select [modeid],@l,right('00000'+ltrim(modeid),5),modename
from tb where parentid=@id
while @@rowcount>0
begin
set @l=@l+1
insert @re
select a.modeid,@l,b.sort+right('00000'+ltrim(a.modeid),5),
b.path+' - '+a.modename
from tb as a,@re as b
where b.id=a.parentid and b.level=@l-1
end
update @re set level = level
return
end
go
select a.modeid,a.parentid,REPLICATE(' ',b.level) +'┝'+a.modename,b.level,b.sort ,b.path from tb a,f_getC(0) b
where a.modeid=b.id
order by sort
/*
modeid parentid level
----------- ----------- -------------------- ----------- -------------------- ----------------------------------------
100 0 ┝商品管理 0 00100 商品管理
107 100 ┝商品管理 1 0010000107 商品管理 - 商品管理
110 107 ┝商品信息管理 2 001000010700110 商品管理 - 商品管理 - 商品信息管理
111 107 ┝商品分类管理 2 001000010700111 商品管理 - 商品管理 - 商品分类管理
112 107 ┝回收站管理 2 001000010700112 商品管理 - 商品管理 - 回收站管理
108 100 ┝明细管理 1 0010000108 商品管理 - 明细管理
114 108 ┝团购管理 2 001000010800114 商品管理 - 明细管理 - 团购管理
115 108 ┝拍卖管理 2 001000010800115 商品管理 - 明细管理 - 拍卖管理
116 108 ┝优惠管理 2 001000010800116 商品管理 - 明细管理 - 优惠管理
109 100 ┝物流管理 1 0010000109 商品管理 - 物流管理
101 0 ┝定单管理 0 00101 定单管理
102 0 ┝用户管理 0 00102 用户管理
117 102 ┝会员管理 1 0010200117 用户管理 - 会员管理
118 102 ┝会员卡管理 1 0010200118 用户管理 - 会员卡管理
119 102 ┝资金管理 1 0010200119 用户管理 - 资金管理
120 102 ┝管理员管理 1 0010200120 用户管理 - 管理员管理
121 120 ┝添加管理员 2 001020012000121 用户管理 - 管理员管理 - 添加管理员
122 120 ┝修改管理员 2 001020012000122 用户管理 - 管理员管理 - 修改管理员
104 0 ┝学院广告 0 00104 学院广告
105 0 ┝系统设置 0 00105 系统设置
106 0 ┝附件管理 0 00106 附件管理
(21 行受影响)
*/
----------
转载:SQL 递归树 子父节点相互查询的更多相关文章
- SQL 递归树 子父节点相互查询
if object_id('[tb]') is not null drop table [tb] go create table [tb]([modeid] int,modename varchar( ...
- sql 递归显示所有父节点
1.我先建两个表 一个表示项目及级别 另一个表示项目最后一级中包含内容.两个表的数据如图 CREATE TABLE [dbo].[yq_Project]( ,) primary key, ) NOT ...
- 基于EasyUi ComBotree树修改 父节点选择问题
本人在使用 Easy UI 期间发现了一个不太适合项目的bug,可能也不算bug把 . 毕竟不同项目背景 取舍不同. 我在做网元树选择的时候 发现当选取父节点后,子节点都会被选择 返回 .但是如 ...
- JavaScript之递归查找所有父节点
......data: () => ({ // 数据 dt: [{ id: '1', children: [ { id: '1-1', children: [ { id: '1-1-1', ch ...
- 使用postgre数据库实现树形结构表的子-父级迭代查询,通过级联菜单简单举例
前言:开发常用的关系型数据库MySQL,mssql,postgre,Oracle,简单的增删改查的SQL语句都与标准SQL兼容,这个不用讲,那么对于迭代查询(不严格的叫法:递归查询)每种数据库都不一样 ...
- 使用layer 弹出对话框 子父页面相互参数传递 父页面获取子页面参数实例
一.先看效果: 1.点击三个点的图标弹出了子页面: 2.子页面调用父页面方法,图一调用父页面方法,图二得到父页面var变量. 3.选择之后,关闭弹框,父页面得到子页面单选框选择的v ...
- iframe子父窗口相互操作方法或元素
一.jquery 父.子页面之间页面元素的获取,方法的调用: 1. 父页面获取子页面元素: 格式:$("#iframe的ID").contents().find("#if ...
- sql 递归树
with CTE as ( -->Begin 一个定位点成员 select ID, PersonName,ParentID,cast(PersonName as nvarchar(max)) a ...
- layer 弹出对话框 子父页面相互参数传递
转载:https://blog.csdn.net/flybridy/article/details/78610737
随机推荐
- mysql-1
接触mysql已经一年多了,但是平时很少用到,仅限于安装部署,最近在学习运维开发,需要用到数据库,于是买了一本mysql必知必会,给自己一个两个星期的时间,学完这本书, 写这一系列的博客,就是记录学习 ...
- shell将标准错误输出重定向到 其他地方
经常可以在一些脚本,尤其是在crontab调用时发现如下形式的命令调用: /tmp/test.sh > /tmp/test.log >& 前半部分/tmp/test.sh > ...
- MySQL入门(四)
我好久没有写这个系列了,也确实不知道写什么.回首我学习数据库的路,最开始搞Oracle的时候其实没有搞懂Oracle,也不知道学了什么,后来学习MySQL尤其是InnoDB才大概入了门了.我最开始学习 ...
- Linux的文件时间
在windows下,一个文件有:创建时间.修改时间.访问时间.而在Linux下,一个文件也有三种时间,分别是:访问时间.修改时间.状态改动时间. 1.访问时间,读一次这个文件的内容,这个时间就会更新. ...
- centos yum源配置
5步搞定yum源配置 作者小波/QQ463431476欢迎转载! 第一步: 卸载原来的yum [root@localhost home]#rpm -qa|grep yum|xargs rpm -e - ...
- speex进行音频去噪
应用speex进行音频去噪,speex功能很强大,因为opus的出现,用speex进行编码/解码的人几乎没有了,但是用speex来进行降噪,去除回声,增益还是很多. 这里用speex进行音频去噪,主要 ...
- nodejs——网络编程模块
net模块提供了一个异步网络包装器,用于TCP网络编程,它包含了创建服务器和客户端的方法.dgram模块用于UDP网络编程. 参考链接:https://nodejs.org/api/net.html, ...
- 【温故而知新-Javascript】使用 Ajax(续)
1. 准备向服务器发送数据 Ajax 最常见的一大用途是向服务器发送数据.最典型的情况是从 客户端发送表单数据,即用户在form元素所含的各个 input 元素里输入的值.下面代码展示了一张简单的表单 ...
- 一个App需要的东西
1.短信申请平台 (发送验证码需要的短信) http://www.yuntongxun.com/api/sms?nl=sy_cp 容联云通讯
- NSArray遍历和修改崩溃
//一.代码 NSArray *array = [self.dataList mutableCopy]; 或 NSArray *array = [NSArray arrayWithArray:self ...