매우 간단한 단일 통화 솔루션을하는 데 도움이됩니다. 전염병과 같은 중첩 된 세트를 피하는 것이 좋습니다 - 교실에있는 것이 가장 좋습니다!
당신이해야 할 일은 당신의 PHP에서 저장된 procs 중 하나를 호출하는 것입니다!
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 ;
"훨씬 더 효율적인 방법"--- 그것은 매우 논의할만한 문구입니다. NS는 종종 트리 수정에서 ** 성능이 떨어집니다 **. – zerkms
zerkms는 정확하지만 자주 수정할 필요가없는 경우 대답이 될 수 있습니다. –
흥미로운 기사, 감사합니다 – Simon