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). 查看深度,
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 里面怎样实现上面四种情形:
前三种都比較简单。能够非常easy写出SQL。
1)查看深度
mysql> SELECT COUNT(DISTINCT IFNULL(parentid,-1)) AS LEVEL FROM country_relation
;
+-------+
| LEVEL |
+-------+
| 4 |
+-------+
1 row in set (0.00 sec)
2)查看ROOT节点
mysql> SELECT b.`name` AS root_node FROM
-> (
-> SELECT id FROM country_relation WHERE parentid IS NULL
-> ) AS a, country AS b WHERE a.id = b.id;
+-----------+
| root_node |
+-----------+
| Earth |
+-----------+
1 row in set (0.00 sec)
3). 查看叶子节点
mysql> SELECT b.`name` AS leaf_node FROM
-> (
-> SELECT id FROM country_relation WHERE id NOT IN (SELECT IFNULL(parentid,
-1) FROM country_relation)
-> ) AS a, country AS b WHERE a.id = b.id;
+-----------------+
| leaf_node |
+-----------------+
| South America |
| Europe |
| Asia |
| Africa |
| Australia |
| Canada |
| Central America |
| Island Nations |
| Alabama |
| Alaska |
| Arizona |
| Arkansas |
| California |
+-----------------+
13 rows in set (0.00 sec)
4) 查看路径
这一块没有简单的SQL实现。只是能够用MySQL的存储过程来实现相同的功能。
存储过程代码例如以下:
DELIMITER $$ USE `t_girl`$$ DROP PROCEDURE IF EXISTS `sp_show_list`$$ CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_show_list`()
BEGIN
-- Created by ytt 2014/11/04.
-- Is equal to oracle's connect by syntax.
-- Body.
DROP TABLE IF EXISTS tmp_country_list;
CREATE TEMPORARY TABLE tmp_country_list (node_level INT UNSIGNED NOT NULL, node_path VARCHAR(1000) NOT NULL);
-- Get the root node.
INSERT INTO tmp_country_list SELECT 1, CONCAT('/',id) FROM country_relation WHERE parentid IS NULL;
-- Loop within all parent node.
cursor1:BEGIN
DECLARE done1 INT DEFAULT 0;
DECLARE i1 INT DEFAULT 1;
DECLARE v_parentid INT DEFAULT -1;
DECLARE v_node_path VARCHAR(1000) DEFAULT '';
DECLARE cr1 CURSOR FOR SELECT parentid FROM country_relation WHERE parentid IS NOT NULL GROUP BY parentid ORDER BY parentid ASC;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done1 = 1; OPEN cr1; loop1:LOOP
FETCH cr1 INTO v_parentid;
IF done1 = 1 THEN
LEAVE loop1;
END IF;
SET i1 = i1 + 1; label_path:BEGIN
DECLARE done2 INT DEFAULT 0;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done2 = 1;
-- Get the upper path.
SELECT node_path FROM tmp_country_list WHERE node_level = i1 - 1 AND LOCATE(v_parentid,node_path) > 0 INTO v_node_path;
-- Escape the outer not found exception.
IF done2 = 1 THEN
SET done2 = 0;
END IF;
INSERT INTO tmp_country_list
SELECT i1,CONCAT(IFNULL(v_node_path,''),'/',id) FROM country_relation WHERE parentid = v_parentid;
END;
END LOOP; CLOSE cr1; END;
-- Update node's id to its real name.
update_name_label:BEGIN
DECLARE cnt INT DEFAULT 0;
DECLARE i2 INT DEFAULT 0;
SELECT MAX(node_level) FROM tmp_country_list INTO cnt;
WHILE i2 < cnt
DO
UPDATE tmp_country_list AS a, country AS b
SET a.node_path = REPLACE(a.node_path,CONCAT('/',b.id),CONCAT('/',b.name))
WHERE LOCATE(CONCAT('/',b.id),a.node_path) > 0;
SET i2 = i2 + 1;
END WHILE;
END; SELECT node_path FROM tmp_country_list;
END$$ DELIMITER ;
调用结果:
mysql> CALL sp_show_list();
+-----------------------------------------------+
| node_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 rows in set (0.04 sec) Query OK, 0 rows affected (0.08 sec)
MySQL 模拟Oracle邻接模型树形处理的更多相关文章
- mysql 模拟oracle中的序列
因业务需要,把oracle 数据据转成mysql,同时oracle中程序本来一直在用 序列, mysql中没有,所以需要在mysql中新建一个表进行模拟, CREATE TABLE `sequence ...
- MySQL模拟Oracle序列使用
https://www.runoob.com/mysql/mysql-using-sequences.html 一篇笔记开始看 注意:创建序列表时一定要有 主键id自增,否则为只读状态不能修改递增 ...
- MySQL实现嵌套集合模型
MySQL实现嵌套集合模型 译文主要是介绍如何用MySQL来存储嵌套集合数据.在其中会增加一些自己的理解,也会删除掉一些自认为无用的废话. 这篇文章主要讲的是嵌套集合模型,所以邻接表不是本文的重点,简 ...
- 【软件实施面试】MySQL和Oracle联合查询以及聚合函数面试总结
软件实施面试系列文章第二弹,MySQL和Oracle联合查询以及聚合函数的面试总结.放眼望去全是MySQL,就不能来点Oracle吗?之前面过不少公司,也做过不少笔试题,现在已经很少做笔试题了.你肚子 ...
- 【无私分享:ASP.NET CORE 项目实战(第十二章)】添加对SqlServer、MySql、Oracle的支持
目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 增加对多数据库的支持,并不是意味着同时对多种数据库操作,当然,后面,我们会尝试同时对多种数据库操作,这可能需要多个上下文,暂且 ...
- qlserver、Mysql、Oracle三种数据库的优缺点总结
这篇文章主要介绍了sqlserver.Mysql.Oracle三种数据库的优缺点总结,需要的朋友可以参考下 一.sqlserver优点:易用性.适合分布式组织的可伸缩性.用于决策支持的数据仓库功能 ...
- MySQL模拟Oralce闪回操作
在前面的文章中我们介绍了MySQL误操作后数据恢复(update,delete忘加where条件),大概操作是通过sed命令把binlog中相关SQL误操作给逆向回来,然后导入SQL文件来恢复错误操作 ...
- 使用动态跟踪技术SystemTap监控MySQL、Oracle性能
[IT168 技术]本文根据吕海波2018年5月11日在[第九届中国数据库技术大会]上的演讲内容整理而成. 讲师介绍: 吕海波,美创科技研究员,ITPUB管理版版主.出版技术书籍<Oracle内 ...
- mysql实现oracle序列
mysql实现oracle序列的方案1.建表,表结构为:drop table if exists sequence; create table sequence ( seq_name VARCHAR( ...
随机推荐
- Swift - 系统声音服务的使用(播放声音,提醒,震动)
1,系统声音服务介绍: 系统声音服务提供了一个Api,用于播放不超过30秒的声音.它支持的文件格式有限,具体的说只有CAF.AIF和使用PCM或IMA/ADPCM数据的WAV文件. 但此函数没有提供操 ...
- NYOJ 1085 数单词 (AC自己主动机模板题)
数单词 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描写叙述 为了可以顺利通过英语四六级考试,如今大家每天早上都会早起读英语. LYH本来以为自己在6月份的考试中能够通过六 ...
- bootstrap之WaitForIdle&&Clear
(上篇文章写完才发现.说好的按顺序可是回头一看全然不是按顺序的)明明WaitForIdle才是第一个.哎,老了,后脑勺不行了. WaitForIdle package io.appium.androi ...
- 怎样改动Myeclipse10.7的Servlet模板
(1)在myeclipse10.0曾经的版本号中咱庄文件夹仅仅有叶仅仅需找到plugins在文件夹下找到: com.genuitec.eclipse.wizards_9.0.0.me201211011 ...
- uva 11732 - strcmp() Anyone? 不错的Trie题
题解:http://blog.csdn.net/u013480600/article/details/23122503 我的代码一直TLE,,,看了人家的之后,认为1.链式前向星比較好,2.*dept ...
- MFC实现多风格真彩色大图标工具栏按钮
研究zlib库,想实现一个类似winrar功能的小东东,打开winrar界面看它的工具栏比较好看于是动手想做一个,当然资源也使用的是winrar附带的.下面是截图:真彩色(32位)32*32大图标工具 ...
- 【Visual C++】绘图函数BitBlt的使用方法
BitBlt 该函数对指定的源设备环境区域中的像素进行位块(bit_block)转换,以传送到目标设备环境. 原型: BOOL BitBlt( HDC hdcDest, int nXDest, ...
- Python 新浪微博元素 (Word, Screen Name)词汇多样性
CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-7-10 @author: guaguastd @name: w ...
- JavaScript学习总结1
/***我是切割线 的開始***/ //利用prototype属性能够加入公有属性和方法 function myConstructor2(){ this.a='大灰狼'; }; //声明构造函数,能够 ...
- C++ 11 右值引用以及std::move
转载请注明出处:http://blog.csdn.net/luotuo44/article/details/46779063 新类型: int和int&是什么?都是类型.int是整数类型,in ...