自关联

作者: 运维开发_西瓜甜 | 来源:发表于2019-08-13 11:17 被阅读0次

https://www.jianshu.com/p/201762e068b2

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;

image.png image.png
image.png
image.png image.png

相关文章

  • 自关联

    https://www.jianshu.com/p/201762e068b2

  • 自关联

  • Flask里面使用mysql数据库关系四大模板

    一对多 多对多 自关联一对多 自关联多对多

  • 自关联和视图

    自关联 float(6,2)实现结果形式0000.00汉字/3字符 建表 查询 视图 1.从原始表导出的表2.他是...

  • Django模型-自关联

    新建模型AreaInfo,生成迁移 class AreaInfo(models.Model): atitle = ...

  • 自关联查询

    自关联 设计省信息的表结构provinces 设计市信息的表结构citys citys表的proid表示城市所属的...

  • Jpa中表自关联

    Jpa中一个实体类对应一张表,但实际应用中表与表之前是有关联的,有时也会关联到自身。 以企业信息表为例,她可能关联...

  • 12.自关联

    -- 查询山东省有哪些城市 --select * from areas as provinve innser jo...

  • MySQL自关联表

    create table node_tree( id int not null auto_increment pr...

  • 自关联,索引,权限

    自关联 当where语句里需要一张表与他自己的其他字段相关联时,就需要使用自关联 索引 唯一索引 删除唯一索引 组...

网友评论

      本文标题:自关联

      本文链接:https://www.haomeiwen.com/subject/fcrvjctx.html