显示存储过程中的简单COUNT(s)

时间:2015-10-23 23:50:44

标签: php mysql stored-procedures

我已经创建了我的第一个(希望)工作的MySQL存储过程,并且我试图弄清楚如何显示" COUNT" - 儿童,孙子等人数

我定位的表格(gz_life_mammals)包含以父子关系排列的科学名称(字段Taxon和Parent),以及数字父ID,如下所示:

Taxon | Parent | ParentID
Mammalia | Chordata | 1
Carnivora | Mammalia | 2
Felidae | Carnivora | 3
Panthera | Felidae | 4
Panthera-leo | Panthera | 5
Panthera-tigris | Panthera | 5
Canidae | Carnivora | 3
Canis | Canidae | 4
Canis-lupus | Canis | 5

所以,如果我访问MySite / life / carnivora,我希望它显示孩子的数量(2 - 猫科和犬科),大孩子(2)和曾祖母的数量(3,所有5级[种类])。如果我访问MySite / life / mammalia,它会显示1个孩子(Carnivora),2个大孩子,2个曾孙子孙女和3个曾孙子孙女。

这是我存储过程的代码:

BEGIN
-- theId parameter means i am anywhere in hierarchy of Taxon
-- and i want all decendent Taxons
declare bDoneYet boolean default false;
declare working_on int;
declare next_level int; -- parent's level value + 1
declare theCount int;

CREATE temporary TABLE xxFindChildenxx
(   -- A Helper table to mimic a recursive-like fetch
    N int not null, -- from OP's table called 'gz_life_mammals'
    processed int not null, -- 0 for not processed, 1 for processed
    level int not null, -- 0 is the id passed in, -1=trying to figure out, 1=children, 2=grandchildren, etc
    ParentID int not null -- helps clue us in to figure out level
    -- NOTE: we don't care about level or parent when N=parameter theId passed into stored proc
    -- in fact we will be deleting that row near the bottom or proc
);

set bDoneYet=false;
insert into xxFindChildenxx (N,processed,level,ParentID) select  theId,0,0,0;  -- prime the pump, get sp parameter in here

-- stay inside below while til all retrieved children/children of  children are retrieved
while (!bDoneYet) do
    -- see if there are any more to process for children
    -- simply look in worktable for ones where processed=0;
    select count(*) into theCount from xxFindChildenxx where processed=0;

    if (theCount=0) then 
        -- found em all, we are done inside this while loop
        set bDoneYet=true;
    else
        -- one not processed yet, insert its children for processing
        SELECT N,level+1 INTO working_on,next_level FROM xxFindChildenxx where processed=0 limit 1; -- order does not matter, just get one

        -- insert the rows where the parent=the one we are processing  (working_on)
        insert into xxFindChildenxx (N,processed,level,ParentID)
        select N,0,next_level,ParentID
        from gz_life_mammals
        where ParentID=working_on;

        -- mark the one we "processed for children" as processed
        -- so we processed a row, but its children rows are yet to be  processed
        update xxFindChildenxx set processed=1 where N=working_on;
    end if;
end while;

delete from xxFindChildenxx where N=theId;  -- don't really need the top level row now (stored proc parameter value)
select level,count(*) as lvlCount from xxFindChildenxx group by level;
drop table xxFindChildenxx;
END

1 个答案:

答案 0 :(得分:1)

这是我为你为你写的答案的副产品。你要做的是,而不是在最后倾倒计数,即儿童的数量,子孙的数量等,请考虑以下因素。

你留下了一个临时表(xxFindChildenxx),里面有id。

您还可以在问题的顶部显示您的分类表gz_life_mammals。你需要在该表中显示的是第一列,auto_inc id表示行主键。

因此,对于其中包含ID的2个表格,您将xxFindChildenxx.ParentId加入gz_life_mammals.id,按level排序

百万美元的问题在于你是否喜欢我给你留下的那种结构。我是朝这个方向开车的:ParentId是Drew(也就是我)组成的id。你最初把它作为varchar或其他东西。所以你需要确定整件事情是否需要像我做的那样颠倒过来,然后回到字符串(不是id' s)