转载: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
随机推荐
- OpenResty(nginx+lua) 入门
OpenResty 官网:http://openresty.org/ OpenResty 是一个nginx和它的各种三方模块的一个打包而成的软件平台.最重要的一点是它将lua/luajit打包了进来, ...
- 关于iOS构建版本提交iTunes后,一直不出现,没加号的解决方案
最近第一次遇到,正常打包,上传iTunes App Store,都能正常upload. 也可能是因为刚升了Xcode 8 的缘故,莫名其妙的小问题... 描述如下: 如果进iTunes的活动界面,也能 ...
- ES6新增值比较函数Object.is
在这之前我们比较值使用两等号 “==” 或 三等号“===”, 三等号更加严格,只要比较两方类型不同立即返回false. 另外,有且只有一个值不和自己相等,它是NaN 现在ES6又加了一个Object ...
- Can't load AMD 64-bit .dll on a IA 32-bit platform
主要谈谈在win8.1(64bit)下搭建环境的经历. 安装win8.1(64bit)后,配置java环境是费了我一番心思的,所以想记录下来,成为经验.64位系统下比较理想的配置应该是 64位jdk ...
- stm32之NVIC
非本人原创,转载自http://blog.csdn.net/denghuanhuandeng/article/details/8350392 STM32的NVIC理解 例程: /* Configur ...
- Windows Azure IP地址详解
Windows Azure上的IP地址有以下几种: 公网IP地址 VIP ILPIP Reserved IP 内网IP地址 DIP Static IP VIP是动态分配的公网IP,VIP可以被分配到云 ...
- Jedis下的ShardedJedis(分布式)使用方法(一)
原来项目中有用到Redis用作缓存服务,刚开始时只用一台Redis就能够满足服务,随着项目的慢慢进行,发现一台满足不了现有的项目需求,因为Redis操作都是原子性这样的特性,造成有时同时读写缓存造成查 ...
- [No00000A]计算机的存储单位
位 bit (比特)(Binary Digits):存放一位二进制数,即 0 或 1,最小的存储单位. 字节 byte:8个二进制[bit (比特)(Binary Digits)]位为一个字节(B), ...
- java 23 - 1 设计模式之工厂方法模式
转载: JAVA设计模式之工厂模式(简单工厂模式+工厂方法模式)
- poj3278 Catch That Cow
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 73973 Accepted: 23308 ...