Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL嵌套的SELECT查询,具有无穷的子项_Mysql_Sql_Database_Parent_Parent Child - Fatal编程技术网

MySQL嵌套的SELECT查询,具有无穷的子项

MySQL嵌套的SELECT查询,具有无穷的子项,mysql,sql,database,parent,parent-child,Mysql,Sql,Database,Parent,Parent Child,我的数据库结构 id content ------------- 2 My content 4 Another content 7 Example content 8 Test content 11 Some content id page_id parent_id -------------------------- 1 2 0 2 4 2 3 7 2 4

我的数据库结构

id    content
-------------
2     My content
4     Another content
7     Example content
8     Test content
11    Some content
id    page_id    parent_id
--------------------------
1     2          0
2     4          2
3     7          2
4     8          7
5     11         8
我从我的数据库开始

页面表

id    content
-------------
2     My content
4     Another content
7     Example content
8     Test content
11    Some content
id    page_id    parent_id
--------------------------
1     2          0
2     4          2
3     7          2
4     8          7
5     11         8
父表

id    content
-------------
2     My content
4     Another content
7     Example content
8     Test content
11    Some content
id    page_id    parent_id
--------------------------
1     2          0
2     4          2
3     7          2
4     8          7
5     11         8
parents\u table.page\u id
连接到
pages\u table.id

问题

  • 我可以用SQL获取第11页的id并爬升该id的所有父级吗 直到我到达父级\u id 0
  • 家长总数不详
可能是一张虚拟桌子?

这就是我能想到的,一张虚拟的桌子。仅仅是一个想法,可能不是正确的方法

id    parent_id_1    parent_id_2    parent_id_3    parent_id_4    parent_id_5
-----------------------------------------------------------------------------
11    8              7              4              2              0

用MySql实现这一点并没有明智而优雅的方法
在Oracle中,您可以使用connect来执行此操作,它可能不是,但是有一种聪明的方法可以解决此问题,而无需使用“connect by”

为什么不使用存储过程

create table hier
(id int, page_id int, parent_id int);
insert into hier values
(1    , 2,          0),
(2   ,  4 ,         2),
(3  ,   7  ,        2),
(4 ,    8   ,       7),
(5,     11   ,      8);


drop procedure if exists getHier;
delimiter $$
create procedure getHier(start int)
begin

select @parent_id:=parent_id from hier where page_id = start;

drop table if exists result;
create temporary table result
(id int primary key auto_increment,
page_id int);

insert into result (page_id) values (@parent_id);

while @parent_id != 0 DO 
insert into result (page_id)
select @parent_id:=parent_id from hier where page_id = @parent_id;

end while;

select page_id from result order by id;
end $$
delimiter ;
那就做吧

call getHier(11)
结果:

page_id
8
7
2
0

顺便说一句,您想要的输出是错误的;)

有什么反馈吗?