统计同级个数是啥意思?
aid 1 = 8 ----- 4+1+3
aid 7 = 5 ------4+1
aid 11 = 4 -----1+3没太明白你这个数据是咋回事.

解决方案 »

  1.   

    select count(a_id) from A group by a_id;
      

  2.   

    因为a_count 我是从其它表得来的
    得到的统计并没有把父级以下的统计相加所以现在要相别
    这样我就可以一目了然能清楚看到那个分类下有那些产品个数了
      

  3.   

    你这个count不对的,得不到我想要的数据
      

  4.   

    不太明白楼主的意思。
    如果统计父ID下面所有子节点下面的个数应该是
    aid 1 = 8 ----- 4+1+3
    aid 7 = 4 ------1+3
    aid 11 = 3 -----3 
      

  5.   


    表A 
    a_id parent_id a_count 
    1      0        4 
    7      1        1 
    11    7        3 
    表A属于无限分类parent_id = 0为最顶级 
    现在问题是 
    如何统计同级的个数 
    比如 a_id 1 = 8 =4+3+1
        a_id 7 = 4  = 1+3 
        a_id 11 = 3 =3
    以此类推
    刚才写错了不好意思
      

  6.   

    无限级,这个处理比较困难哈.
    期待高手ing...
      

  7.   


    function getcount($id)
    {
    $con = mysql_connect("localhost","user","userpwd");
    mysql_select_db("dedecms_ewoka", $con);
    mysql_query('SET NAMES utf8');
    $reult=mysql_query("select a_id,a_count from tbl_A where parent_id=$id",$con);
    if ($value = mysql_fetch_array($reult))
    {
    $count += $value['a_count']+getcount($value['a_id']);

    return $count;
    }
    echo 'a_id 1=',getcount(1),'<br/>';
    echo 'a_id 7=',getcount(7),'<br/>';
    echo 'a_id 11=',getcount(11),'<br/>';a_id 1 :getcount(1)
    a_id 7 :getcount(7)
    a_id 11 :getcount(11)
    递归获取,在你的程序里面可能还要用for来读对应每一个的。
      

  8.   

    那你的id 和 parent_id 用int 类型的:
    查看 aid 1:
       select count(*) from $table where parent_id<=0 ;
      

  9.   


    //把结果集数组整理这个样子
    $result[1] = array('a_id'=>1,'parent_id'=>0,'a_count'=>4);
    $result[7] = array('a_id'=>7,'parent_id'=>1,'a_count'=>1);
    $result[11] = array('a_id'=>11,'parent_id'=>7,'a_count'=>3);
    //try these codes...
    $a = array();
    foreach($result as $k=>$v)
    {
    $a[$v['a_id']] = $v['a_count'];
    $o = $v['parent_id']; if($result[$o]) 
    {
    $a[$o] +=  $v['a_count'];
    while($result[$o]['parent_id'] != 0)
    {
    $a[$result[$o]['parent_id']] += $v['a_count'];
    $o = $result[$o]['parent_id'];
    }
    }
    }print_r($a);
      

  10.   

    在你的表中再加一个字段,就叫a_count2吧,每次新增产品的时候
    将对应的父类别的a_count2也加1,这样的话最多4次数据库访问!