博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sql树形结构
阅读量:6923 次
发布时间:2019-06-27

本文共 1111 字,大约阅读时间需要 3 分钟。

create temporary table `treeView`(  `id` varchar(20),  `org_name` varchar(20),  `parent_org_id` varchar(20),  `org_code` varchar(20))insert into treeView(id,org_name,parent_org_id,org_code)values('1','根',null,'1');insert into treeView(id,org_name,parent_org_id,org_code)values('2','机构1','1','2');insert into treeView(id,org_name,parent_org_id,org_code)values('3','机构1—1','2','2');insert into treeView(id,org_name,parent_org_id,org_code)values('4','机构2','1','1');insert into treeView(id,org_name,parent_org_id,org_code)values('5','机构2-1','4','1');insert into treeView(id,org_name,parent_org_id,org_code)values('6','机构1-2','2','1');create temporary table `tree`(    select id,org_name,parent_org_id,org_code from treeView where parent_org_id in (null,'1'))insert into tree(id,org_name,parent_org_id,org_code)select id,org_name,parent_org_id,org_code from treeUNION Allselect t.id,t.org_name,t.parent_org_id,t.org_code from treeView as t,tree as v    where t.parent_org_id = v.id order by t.org_code    DROP TABLE treeView    DROP TABLE treeselect * from tree

 

转载于:https://www.cnblogs.com/wqff-biubiu/p/9082779.html

你可能感兴趣的文章
浅谈正则表达式
查看>>
win7下安装linux操作系统,实现双系统
查看>>
Centos7简单初始化脚本
查看>>
RabbitMQ学习总结(2)——安装、配置与监控
查看>>
嵌入式Linux ARM汇编(六)——GNU ARM汇编编程
查看>>
大型网站技术架构(五)网站高可用架构
查看>>
linux下利用curl监控网页shell脚本
查看>>
log4j 2使用教程
查看>>
Spring常用注解
查看>>
Maven学习总结(三)——使用Maven构建项目
查看>>
js比较两个"日期时间"的大小
查看>>
sshd_config 中文手册
查看>>
前端常用工具记录
查看>>
Spring学习总结(4)——Spring AOP教程
查看>>
scanf函数详解(上)
查看>>
SCVMM 2012 R2创建逻辑网络和VM网络
查看>>
SCVMM 2012 R2之添加hyper-v主机
查看>>
pfSense book之用户管理和认证
查看>>
在pfSense中配置Wi-Fi + Lan桥接接入点
查看>>
【译】如何通过简单的3步恢复Windows7同时删除Ubuntu
查看>>