MySQL自关联表

MySQL自关联表,第1张

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')

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'

字段:id ,name ,friend_id,hit_id,like_id

关联:friend_id为A表id,hit_id为A表id

要求查询结果如下:

id ,name ,friend_id关联的name,hit_id关联的name,hit_id关联的name,like_id关联的name!


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/8632410.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-19
下一篇 2023-04-19

发表评论

登录后才能评论

评论列表(0条)

保存