自关联

2019-08-13  本文已影响0人  光明_7c13
create table  node_tree(
   id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   node_name varchar(128) NOT NULL DEFAULT '',
   up_node_id int,
   node_level char(1)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci;

insert into node_tree(node_name,up_node_id,node_level)
    values 
    ('jx', NULL, '1'),
    ('jx.webserver', 1, '2'),
    ('jx.webserver.nginx1', 2, '3'),
    ('jx.logserver', 1, '2')


insert into node_tree(node_name,up_node_id,node_level)
    values 
    ('jx.logserver.logstash1', 4, '3')





create table  node_tree1(
   id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   name varchar(128) NOT NULL DEFAULT '',
   level char(1)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci;


create table  node_tree2(
   id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   name varchar(128) NOT NULL DEFAULT '',
   up_id int,
   level char(1)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci;




insert into node_tree1(name, level)
    values 
    ('yx', '1');

insert into node_tree2(name, up_id, level)
    values 
    ('jx.webserver', 1, '2'),
    ('jx.logserver', 1, '2')


insert into node_tree1(name, level)
    values 
    ('xs', '1');

insert into node_tree2(name, up_id, level)
    values 
    ('xs.webserver', 2, '2');




select 
node_tree1.id as  主表ID,
node_tree1.name as 主表名字, 
node_tree2.name  as 从表名字,
node_tree2.up_id  as 从表上级ID 

from node_tree1, node_tree2 

where node_tree1.name='jx';



select 
node_tree1.id as  主表ID,
node_tree1.node_name as 主表名字, 
node_tree2.node_name  as 从表名字,
node_tree2.up_node_id  as 从表上级ID 

from node_tree as node_tree1, node_tree as node_tree2 

where node_tree1.node_name='jx'
and node_tree1.id = node_tree2.up_node_id;




上一篇下一篇

猜你喜欢

热点阅读