0

0

【原创】MySQL 模拟Oracle邻接模型树形处理

php中文网

php中文网

发布时间:2016-06-07 16:48:17

|

1217人浏览过

|

来源于php中文网

原创

数据库对层次结构的处理模型有好多种,可以根据自己的需求来设计模型,当然最简单的也是最容易设计的模型就是所谓的邻接模型。在这方面,其他数据库比如Oracle提

数据库对层次结构的处理模型有好多种,可以根据自己的需求来设计模型,,当然最简单的也是最容易设计的模型就是所谓的邻接模型。在这方面,其他数据库比如oracle 提供了现成的分析方法 connect by,而mysql在这方面就显得有些薄弱了。 不过可以用mysql的存储过程实现oracle类似的分析功能


这样,先来创建一个简单的数表。

create table country ( id number(2) not null,  name varchar(60) not null); create table country_relation (id number(2),  parentid number(2));


插入一些数据

-- Table country. insert into country (id,name) values (0,'Earth'); insert into country (id,name) values (2,'North America'); insert into country (id,name) values (3,'South America'); insert into country (id,name) values (4,'Europe'); insert into country (id,name) values (5,'Asia'); insert into country (id,name) values (6,'Africa'); insert into country (id,name) values (7,'Australia'); insert into country (id,name) values (8,'Canada'); insert into country (id,name) values (9,'Central America'); insert into country (id,name) values (10,'Island Nations'); insert into country (id,name) values (11,'United States'); insert into country (id,name) values (12,'Alabama'); insert into country (id,name) values (13,'Alaska'); insert into country (id,name) values (14,'Arizona'); insert into country (id,name) values (15,'Arkansas'); insert into country (id,name) values (16,'California');-- Table country_relation. insert into country_relation (id,parentid) values (0,NULL); insert into country_relation (id,parentid) values (2,0); insert into country_relation (id,parentid) values (3,0); insert into country_relation (id,parentid) values (4,0); insert into country_relation (id,parentid) values (5,0); insert into country_relation (id,parentid) values (6,0); insert into country_relation (id,parentid) values (7,0); insert into country_relation (id,parentid) values (8,2); insert into country_relation (id,parentid) values (9,2); insert into country_relation (id,parentid) values (10,2); insert into country_relation (id,parentid) values (11,2); insert into country_relation (id,parentid) values (12,11); insert into country_relation (id,parentid) values (13,11); insert into country_relation (id,parentid) values (14,11); insert into country_relation (id,parentid) values (15,11); insert into country_relation (id,parentid) values (16,11);



在Oracle 里面,对这些操作就比较简单了,都是系统提供的。

比如下面四种情形:

1). 查看深度,

Play.ht
Play.ht

根据文本生成多种逼真的语音

下载
select max(level) "level" from COUNTRY_RELATION a start with a.parentid is NULL connect by PRIOR a.id = a.PARENTID order by level;      level ----------          4 已用时间:  00: 00: 00.03

2). 查看叶子节点

select name from  ( select b.name, connect_by_isleaf "isleaf" from COUNTRY_RELATION a inner join country b on (a.id = b.id)  start with a.parentid is NULL connect by prior a.id = a.PARENTID  ) T where T."isleaf" = 1; NAME -------------------------------------------------- Canada Central America Island Nations Alabama Alaska Arizona Arkansas California South America Europe Asia Africa Australia 已选择13行。 已用时间:  00: 00: 00.01


3) 查看ROOT节点

select connect_by_root b.name from COUNTRY_RELATION a inner join country b on (a.id = b.id)  start with a.parentid is NULL connect by a.id = a.PARENTID  CONNECT_BY_ROOTB.NAME -------------------------------------------------- Earth 已用时间:  00: 00: 00.01

4). 查看路径

select sys_connect_by_path(b.name,'/') "path"  from COUNTRY_RELATION a inner join country b on (a.id = b.id)  start with a.parentid is NULL connect by prior a.id = a.PARENTID  order by level,a.id; path -------------------------------------------------- /Earth /Earth/North America /Earth/South America /Earth/Europe /Earth/Asia /Earth/Africa /Earth/Australia /Earth/North America/Canada /Earth/North America/Central America /Earth/North America/Island Nations /Earth/North America/United States /Earth/North America/United States/Alabama /Earth/North America/United States/Alaska /Earth/North America/United States/Arizona /Earth/North America/United States/Arkansas /Earth/North America/United States/California 已选择16行。 已用时间:  00: 00: 00.01



接下来我们看看在MySQL 里面如何实现上面四种情形:

前三种都比较简单,可以很容易写出SQL。

相关专题

更多
苹果官网入口直接访问
苹果官网入口直接访问

苹果官网直接访问入口是https://www.apple.com/cn/,该页面具备0.8秒首屏渲染、HTTP/3与Brotli加速、WebP+AVIF双格式图片、免登录浏览全参数等特性。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

115

2025.12.24

拼豆图纸在线生成器
拼豆图纸在线生成器

拼豆图纸生成器有PixelBeads在线版、BeadGen和“豆图快转”;推荐通过pixelbeads.online或搜索“beadgen free online”直达官网,避开需注册的诱导页面。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

82

2025.12.24

俄罗斯搜索引擎yandex官方入口地址(最新版)
俄罗斯搜索引擎yandex官方入口地址(最新版)

Yandex官方入口网址是https://yandex.com。用户可通过网页端直连或移动端浏览器直接访问,无需登录即可使用搜索、图片、新闻、地图等全部基础功能,并支持多语种检索与静态资源精准筛选。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

546

2025.12.24

JavaScript ES6新特性
JavaScript ES6新特性

ES6是JavaScript的根本性升级,引入let/const实现块级作用域、箭头函数解决this绑定问题、解构赋值与模板字符串简化数据处理、对象简写与模块化提升代码可读性与组织性。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

150

2025.12.24

php框架基础知识汇总
php框架基础知识汇总

php框架是构建web应用程序的架构,提供工具和功能,以简化开发过程。选择合适的框架取决于项目需求和技能水平。实战案例展示了使用laravel构建博客的步骤,包括安装、创建模型、定义路由、编写控制器和呈现视图。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

20

2025.12.24

Word 字间距调整方法汇总
Word 字间距调整方法汇总

本专题整合了Word字间距调整方法,阅读下面的文章了解更详细操作。

47

2025.12.24

任务管理器教程
任务管理器教程

本专题整合了任务管理器相关教程,阅读下面的文章了解更多详细操作。

7

2025.12.24

AppleID格式
AppleID格式

本专题整合了AppleID相关内容,阅读专题下面的文章了解更多详细教程。

12

2025.12.24

csgo视频观看入口合集
csgo视频观看入口合集

本专题整合了csgo观看入口合集,阅读下面的文章了知道更多入口地址。

371

2025.12.24

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
MySQL 教程
MySQL 教程

共48课时 | 1.4万人学习

MySQL 初学入门(mosh老师)
MySQL 初学入门(mosh老师)

共3课时 | 0.3万人学习

简单聊聊mysql8与网络通信
简单聊聊mysql8与网络通信

共1课时 | 771人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号