多分类个数count统计 统计同级个数是啥意思?aid 1 = 8 ----- 4+1+3aid 7 = 5 ------4+1aid 11 = 4 -----1+3没太明白你这个数据是咋回事. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 select count(a_id) from A group by a_id; 因为a_count 我是从其它表得来的得到的统计并没有把父级以下的统计相加所以现在要相别这样我就可以一目了然能清楚看到那个分类下有那些产品个数了 你这个count不对的,得不到我想要的数据 不太明白楼主的意思。如果统计父ID下面所有子节点下面的个数应该是aid 1 = 8 ----- 4+1+3aid 7 = 4 ------1+3aid 11 = 3 -----3 表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以此类推刚才写错了不好意思 无限级,这个处理比较困难哈.期待高手ing... 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来读对应每一个的。 那你的id 和 parent_id 用int 类型的:查看 aid 1: select count(*) from $table where parent_id<=0 ; //把结果集数组整理这个样子$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); 在你的表中再加一个字段,就叫a_count2吧,每次新增产品的时候将对应的父类别的a_count2也加1,这样的话最多4次数据库访问! 求助一下!就用file函数要全部读取怎么读取啊? php调用动态库问题(php扩展) php验证码问题?救命啊,新手 thinkphp分组查询数据求解决 请教EOT输出问题 网页使用curl_multi多线程后速度变得更慢了? smarty的问题??? 求一个好点的用session做的php购物车,急...在线等 (分不够可再加) Session消失不了怎么办? 请问怎么把' " , 写如数据库 PHP下放盗链问题 while 循环问题,在线给分
得到的统计并没有把父级以下的统计相加所以现在要相别
这样我就可以一目了然能清楚看到那个分类下有那些产品个数了
如果统计父ID下面所有子节点下面的个数应该是
aid 1 = 8 ----- 4+1+3
aid 7 = 4 ------1+3
aid 11 = 3 -----3
表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
以此类推
刚才写错了不好意思
期待高手ing...
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来读对应每一个的。
查看 aid 1:
select count(*) from $table where parent_id<=0 ;
//把结果集数组整理这个样子
$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);
将对应的父类别的a_count2也加1,这样的话最多4次数据库访问!