我是存储过程的新手,正在尝试实现@ Hierarchical Query in MySQL II问题的解决方案。我在下面发布了我的代码。我立即挂了第三行。看起来我的问题已经回答@ Error declaring integer variable inside MySQL stored function,但我仍然无法让它发挥作用。
对于我的第一个实验,我想通过将1(动物王国)指定为整数来查询整个分类树。
我一直在查看一些存储过程教程,但目前还不清楚它究竟是如何工作的,而且我可能在马前有购物车。据我了解,可以通过MySQL或PHP查询存储过程,我更愿意在PHP中进行查询。我不明白下面的代码是否是存储过程查询或我在编写查询之前必须做的事情。我已经创建了练习表(t)。
所以也许我应该以另一种方式问我的问题:如果我已经有一个带有父ID的字段的练习表,并且我想学习如何用PHP查询该表,我是否还需要弄乱代码下面是为了创建一个“存储过程”?或者我可以使用PHP编写的查询创建存储过程吗?
DELIMITER $$
create procedure showHierarchyUnder
(
SET i = 1;
)
BEGIN
declare bDoneYet boolean default false;
declare working_on int;
declare next_level int;
declare theCount int;
CREATE temporary TABLE xxFindChildenxx
( N int not null,
processed int not null,
level int not null,
parent int not null
);
set bDoneYet=false;
insert into xxFindChildenxx (N,processed,level,parent) select theId,0,0,0;
while (!bDoneYet) do
select count(*) into theCount from xxFindChildenxx where processed=0;
if (theCount=0) then
set bDoneYet=true;
else
SELECT N,level+1 INTO working_on,next_level FROM xxFindChildenxx where processed=0 limit 1;
insert into xxFindChildenxx (N,processed,level,parent)
select N,0,next_level,parent
from t
where parent=working_on;
update xxFindChildenxx set processed=1 where N=working_on;
end if;
end while;
delete from xxFindChildenxx where N=theId;
select level,count(*) as lvlCount from xxFindChildenxx group by level;
drop table xxFindChildenxx;
END
??
答案 0 :(得分:1)
这将为您创建存储过程。要在存储过程中设置变量,必须先声明它,然后才能在声明所有变量后设置它。请注意SET i = 1;
之间的()
和i
之间的declared i int;
之间的关系是create procedure showHierarchyUnder(IN i int(10))
。看起来你试图传递一个像i
这样的变量但是在调用过程时会期望一个值。你仍然可以这样做,但如果1
永远是DELIMITER //
create procedure showHierarchyUnder()
BEGIN
declare bDoneYet boolean default false;
declare working_on int;
declare next_level int;
declare theCount int;
declare i int;
SET i = 1;
CREATE temporary TABLE xxFindChildenxx
( N int not null,
processed int not null,
level int not null,
parent int not null
);
set bDoneYet=false;
insert into xxFindChildenxx (N,processed,level,parent) select theId,0,0,0;
while (!bDoneYet) do
select count(*) into theCount from xxFindChildenxx where processed=0;
if (theCount=0) then
set bDoneYet=true;
else
SELECT N,level+1 INTO working_on,next_level FROM xxFindChildenxx where processed=0 limit 1;
insert into xxFindChildenxx (N,processed,level,parent)
select N,0,next_level,parent
from t
where parent=working_on;
update xxFindChildenxx set processed=1 where N=working_on;
end if;
end while;
delete from xxFindChildenxx where N=theId;
select level,count(*) as lvlCount from xxFindChildenxx group by level;
drop table xxFindChildenxx;
END//
DELIMITER ;
,那就没有意义了。将来如有疑问,请转到dev.mysql。
RewriteRule ^All\s2009\sfiles/(.*) /archives/old-website/All\ 2009\ Files/$1 [NC,R=301,L]