使用查询选择祖先,如递归函数

问题描述 投票:4回答:2

我有一个数据库表菜单有id, name and parentid

我在数据库中有以下值,我想使用查询收集包括父菜单在内的所有字段。

   id  name      parentid
   1   File         0
   2   New          1
   3   Document     2
   4   Image        2
   5   Edit         0
   6   Copy         5
   7   Paste        5

示例:我有2作为我当前的菜单,我想选择所有具有父Id 2及其父母及其父母父母的字段,直到我到达顶级父母(即使用parentid=0)。

是否可以使用单个查询收集它?如果是,如何实现?

mysql database
2个回答
4
投票

如果你可以控制你的数据结构,那么有一种更好的方法来存储这些数据,然后让你做你需要的东西,这比你试着继续下去要容易得多。

您正在做的通常称为邻接列表模型。您应该查看嵌套集模型,这是一种更有效的存储和检索分层数据的方法。

有一个good tutorial here和网上快速搜索Joe Celko会给你很多关于正确方向的链接,因为他多年来一直在写这篇文章。

希望这可以帮助


4
投票

相当简单的单一调用解决方案,它使用具有非递归存储过程的邻接列表实现。建议避免像瘟疫这样的嵌套设置 - 最好留在教室那些!

您需要做的就是从您的PHP调用这些存储过程中的一个!

call menus_hier_downward(1);
call menus_hier_upward(3);

简单 - 希望它有帮助:)

示例结果

call menus_hier_downward(1);
+---------+-----------+-----------+------------------+-------+
| menu_id | menu_name | parent_id | parent_menu_name | depth |
+---------+-----------+-----------+------------------+-------+
|       1 | File      |      NULL | NULL             |     0 |
|       2 | New       |         1 | File             |     1 |
|       3 | Document  |         2 | New              |     2 |
|       4 | Image     |         2 | New              |     2 |
+---------+-----------+-----------+------------------+-------+
4 rows in set (0.00 sec)

call menus_hier_upward(3);
+---------+-----------+-----------+------------------+-------+
| menu_id | menu_name | parent_id | parent_menu_name | depth |
+---------+-----------+-----------+------------------+-------+
|       3 | Document  |         2 | New              |     1 |
|       2 | New       |         1 | File             |     2 |
|       1 | File      |      NULL | NULL             |     3 |
+---------+-----------+-----------+------------------+-------+
3 rows in set (0.00 sec)

我已经为您提供了两个示例存储过程。一个向下工作另一个向上工作。完整脚本如下:

示例表

drop table if exists menus;
create table menus
(
menu_id smallint unsigned not null auto_increment primary key,
name varchar(255) not null,
parent_id smallint unsigned null,
key (parent_id)
)
engine = innodb;

insert into menus (name, parent_id) values
('File',null), 
 ('New',1), 
   ('Document',2), 
   ('Image',2), 
('Edit',null), 
 ('Copy',5), 
 ('Paste',5);

向下存储过程

drop procedure if exists menus_hier_downward;

delimiter #

create procedure menus_hier_downward
(
in p_menu_id smallint unsigned
)
begin

declare v_done tinyint unsigned default(0);
declare v_dpth smallint unsigned default(0);

create temporary table hier(
 parent_id smallint unsigned, 
 menu_id smallint unsigned, 
 depth smallint unsigned
)engine = memory;

insert into hier select parent_id, menu_id, v_dpth from menus where menu_id = p_menu_id;

/* http://dev.mysql.com/doc/refman/5.0/en/temporary-table-problems.html */

create temporary table tmp engine=memory select * from hier;

while not v_done do

    if exists( select 1 from menus m inner join hier on m.parent_id = hier.menu_id and hier.depth = v_dpth) then

        insert into hier select m.parent_id, m.menu_id, v_dpth + 1 
            from menus m inner join tmp on m.parent_id = tmp.menu_id and tmp.depth = v_dpth;

        set v_dpth = v_dpth + 1;            

        truncate table tmp;
        insert into tmp select * from hier where depth = v_dpth;

    else
        set v_done = 1;
    end if;

end while;

select 
 m.menu_id,
 m.name as menu_name,
 p.menu_id as parent_id,
 p.name as parent_menu_name,
 hier.depth
from 
 hier
inner join menus m on hier.menu_id = m.menu_id
left outer join menus p on hier.parent_id = p.menu_id;

drop temporary table if exists hier;
drop temporary table if exists tmp;

end #

delimiter ;

向上存储过程

drop procedure if exists menus_hier_upward;

delimiter #

create procedure menus_hier_upward
(
in p_menu_id smallint unsigned
)
begin

declare v_done tinyint unsigned default(0);
declare v_dpth smallint unsigned default(0);

create temporary table hier(
 parent_id smallint unsigned, 
 menu_id smallint unsigned, 
 depth smallint unsigned
)engine = memory;

insert into hier select menu_id, null, v_dpth from menus where menu_id = p_menu_id;

/* http://dev.mysql.com/doc/refman/5.0/en/temporary-table-problems.html */

create temporary table tmp engine=memory select * from hier;

while not v_done do

    if exists( select 1 from menus m inner join hier on m.menu_id = hier.parent_id and hier.depth = v_dpth) then

        insert into hier select m.parent_id, m.menu_id, v_dpth + 1 
            from menus m inner join tmp on m.menu_id = tmp.parent_id and tmp.depth = v_dpth;

        set v_dpth = v_dpth + 1;            

        truncate table tmp;
        insert into tmp select * from hier where depth = v_dpth;

    else
        set v_done = 1;
    end if;

end while;

select 
 m.menu_id,
 m.name as menu_name,
 p.menu_id as parent_id,
 p.name as parent_menu_name,
 hier.depth
from 
 hier
inner join menus m on hier.menu_id = m.menu_id
left outer join menus p on hier.parent_id = p.menu_id;

drop temporary table if exists hier;
drop temporary table if exists tmp;

end #

delimiter ;
© www.soinside.com 2019 - 2024. All rights reserved.