Last active
March 27, 2019 05:31
-
-
Save jhsea3do/f733e5ab9677d541e0ab26887dcdbbe6 to your computer and use it in GitHub Desktop.
multiple-column-tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DROP TABLE IF EXISTS `xtree`; | |
CREATE TABLE IF NOT EXISTS `xtree` ( | |
gid BIGINT, | |
line BIGINT, | |
id VARCHAR(3), | |
comments VARCHAR(10), | |
level BIGINT ); | |
insert into `xtree` values ( 1, 1, 'A', 'A', 1); | |
insert into `xtree` values ( 1, 2, 'B', 'B', 2); | |
insert into `xtree` values ( 1, 3, 'E', 'E', 3); | |
insert into `xtree` values ( 1, 4, 'F', 'F', 3); | |
insert into `xtree` values ( 1, 5, 'C', 'C', 2); | |
insert into `xtree` values ( 1, 6, 'G', 'G', 3); | |
insert into `xtree` values ( 1, 7, 'D', 'D', 2); | |
insert into `xtree` values ( 1, 8, 'H', 'H', 3); | |
insert into `xtree` values ( 1, 9, 'J', 'J', 4); | |
insert into `xtree` values ( 1, 10, 'K', 'K', 4); | |
insert into `xtree` values ( 1, 11, 'L', 'L', 4); | |
insert into `xtree` values ( 1, 12, 'I', 'I', 3); | |
insert into `xtree` values ( 1, 13, 'END', 'END', 0); | |
insert into `xtree` values ( 2, 1, 'A', 'A', 1); | |
insert into `xtree` values ( 2, 2, 'B', 'B', 2); | |
insert into `xtree` values ( 2, 3, 'E', 'E', 3); | |
insert into `xtree` values ( 2, 4, 'F', 'F', 3); | |
insert into `xtree` values ( 2, 5, 'C', 'C', 2); | |
insert into `xtree` values ( 2, 6, 'G', 'G', 3); | |
insert into `xtree` values ( 2, 7, 'D', 'D', 2); | |
insert into `xtree` values ( 2, 8, 'H', 'H', 3); | |
insert into `xtree` values ( 2, 9, 'J', 'J', 4); | |
insert into `xtree` values ( 2, 10, 'K', 'K', 4); | |
insert into `xtree` values ( 2, 11, 'L', 'L', 4); | |
insert into `xtree` values ( 2, 12, 'I', 'I', 3); | |
insert into `xtree` values ( 2, 13, 'END', 'END', 0); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- get node and all sub nodes | |
select * from xtree | |
where gid = 1 and line>=8 | |
and line < ( | |
select min(line) from xtree | |
where gid = 1 and line>8 and level<=3 | |
) | |
-- get only sub nodes | |
select * from xtree | |
where gid = 1 and line>=8 and level=(3+1) | |
and line < ( | |
select min(line) from xtree | |
where gid = 1 and line>8 and level<=3 | |
) | |
-- get node and all parent nodes and reverse level | |
select x.gid, x.id, x.comments, x.line, x.level, | |
abs(x.level - p.blv ) as reverse | |
from ( | |
select max(t.line) as line, t.level, t.gid, u.level as blv | |
from ( | |
select * from `xtree` | |
where gid = 1 and id = 'I' | |
) u inner join `xtree` t | |
on u.gid = t.gid | |
and u.line >= t.line | |
and u.level >= t.level | |
group by t.level, t.gid | |
) p inner join `xtree` x | |
on p.gid = x.gid | |
and p.level = x.level | |
and p.line = x.line |
Author
jhsea3do
commented
Mar 27, 2019
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment