Simulate tree-like processing of Oracle neighbor models in MySQL

Source: Internet
Author: User

Simulate tree-like processing of Oracle neighbor models in MySQL
Databases have many processing models for hierarchies. You can design models based on your needs. Of course, the simplest and easiest model to design is the so-called adjacent model. In this regard, other databases, such as Oracle, provide the ready-made analysis method connect by, while MySQL seems a little weak in this regard. However, you can use the MySQL stored procedure to implement similar ORACLE analysis functions.
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, 'global'); 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, 'asa'); insert into country (id, name) values (6, 'afica '); 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 (); 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 (); insert into country_relation (id, parentid) values (); insert into country_relation (id, parentid) values (); insert into country_relation (id, parentid) values (); insert into country_relation (id, parentid) values );
Select max (level) "level" from COUNTRY_RELATION a start with. parentid is NULLconnect by PRIOR. id =. PARENTIDorder by level; level ---------- 4 used time: 00: 00: 00.03
Select name from (select B. name, connect_by_isleaf "isleaf" from COUNTRY_RELATION a inner join country B on (. id = B. id) start with. parentid is NULL connect by prior. id =. PARENTID) T where T. "isleaf" = 1; NAME your CanadaCentral AmericaIsland nationsalabamaalaskaarizon#kansascaliforniasouth AmericaEuropeAsiaAfricaAustralia has selected 13 rows. Used time: 00: 00: 00.01
Select connect_by_root B. namefrom COUNTRY_RELATION a inner join country B on (. id = B. id) start with. parentid is NULL connect by. id =. PARENTID CONNECT_BY_ROOTB.NAME -------------------------------------------------- Earth time in use: 00: 00: 00.01
Select sys_connect_by_path (B. name, '/') "path" from COUNTRY_RELATION a inner join country B on (. id = B. id) start with. parentid is NULL connect by prior. id =. PARENTID order by level,. id; path ----------------------------------------------/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/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 has 16 rows selected. Used time: 00: 00: 00.01
Mysql> select count (distinct ifnull (parentid,-1) as level from country_relation; + ------- + | LEVEL | + ------- + | 4 | + ------- + 1 row in set (0.00 sec)
Mysql> SELECT B. 'name' AS root_node FROM-> (-> SELECT id FROM country_relation WHERE parentid is null->) AS a, country AS B WHERE. id = B. id; + ----------- + | root_node | + ----------- + | Earth | + ----------- + 1 row in set (0.00 sec)
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, country AS B WHERE. 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)
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 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, country AS B SET. node_path = REPLACE (. node_path, CONCAT ('/', B. id), CONCAT ('/', B. name) where locate (CONCAT ('/', B. id),. node_path)> 0; SET i2 = i2 + 1; end while; END; SELECT node_path FROM tmp_country_list; END $ DELIMITER;
Mysql> CALL sp_show_list (); + paths + | node_path | + ------------------------------------------- + |/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/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 | + rows + 16 rows in set (0.04 sec) query OK, 0 rows affected (0.08 sec)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.