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

标签 php mysql stored-procedures

我已经创建了我的第一个(希望如此)工作的 MySQL 存储过程,我正在尝试弄清楚如何显示“COUNT”—— child 、孙子等的数量。

我定位的表 (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,我希望它显示 child 的数量(2 - Felidae 和 Canidae)、孙子的数量(2)和曾孙的数量(3,所有5级[物种])。如果我访问 MySite/life/mammalia,它会显示 1 个 child (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

最佳答案

这是我为您写的关于计数的答案的衍生内容。你想要做的是,而不是在最后倾倒计数,即 child 的计数,孙子的计数等,考虑以下内容。

您剩下一个临时表 (xxFindChildenxx),其中包含 id。

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

因此,对于这 2 个表,它们都包含 id,您将 xxFindChildenxx.ParentId 连接回 gz_life_mammals.id,按 level 排序

百万美元的问题是您是否喜欢我留给您的结构。我正朝着这个方向前进:ParentId 是 Drew(也就是我)编造的一个 ID。您最初将它作为 varchar 或其他东西。所以你需要确定整个事情是否需要像我那样颠倒过来,然后回到字符串(不是 id 的)

关于php - 从存储过程中显示简单的 COUNT(s),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33313021/

相关文章:

mysql - 如何在 Rails 3.2.x 中关闭 MySQL 严格模式

php - 带有游标的存储过程以奇怪的方式执行

java - 从 Java Servlet 调用 Firebird 存储过程

php - 考虑到 Gmail (user.name+label@gmail.com),如何在 PHP 中检查重复的电子邮件地址

php - 如何更改 WordPress 中的图片作者?

php - 将 phpMyAdmin 中的表回显到 html 表中

php - cURL 在一些失败的请求后随机抛出 "curl: (35) OpenSSL SSL_connect: SSL_ERROR_SYSCALL"

php - 合并来自 MySQL 记录的数组

PHP Google Api 客户端 SQL 管理员未启用身份验证 IP

c# - 通过引用传递到 C# 代码时,可空值不会被修改