douyeyan0650 2012-03-14 22:27
浏览 28
已采纳

计算一个类别中的所有子类别

I have database with following columns:

id, parent, name, description

The idhas been set to auto incerement. I want to get total number of all the subcategories that are inisde the parent category (which has it's parent set to 0) I also want this to be reccursive. What can be the fastes way of doing so?

I already wrote the following function but I can not test it at the moment:

function CountDeepSubCategories($parentID, $max = 0)
{
    global $_DB;

    $all = $all + $max;

    $q = $_DB->Query("SELECT id FROM category_cats WHERE parent = $parentID");
    $id = $_DB->GetResultValue($q, 0, 'id');

    $num = $_DB->GetResultNumber($q);       

    for($i = 0; $i < $num; $i++)
    {
         CountDeepSubCategories($id, $all);
    }

    return $all;        
}
  • 写回答

4条回答 默认 最新

  • donglu8549 2012-03-14 22:37
    关注

    does every category have a parent? if that you can do "select count(*) from category_cats" :)

    ok, for the second comment that you've posted, why dont you just add an insider query like this:

    select cct.id,
        (select count(1) from category_cats where parent=cct.id) as subcount
    from category_cats cct
    

    i've not tested it but it should work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看