Mysql 如何获得多级营销(树)子计数
我使用codeigniter作为前端mysql作为后端创建了一个传销项目 在这里,我怀疑是否有儿童计数 这是我的表格流程: 这是我的树流: 这是我的查询如何获得多级营销(树)子计数 例如: 主用户有14个子用户 我尝试了以下代码: 视图: 型号:Mysql 如何获得多级营销(树)子计数,mysql,codeigniter,Mysql,Codeigniter,我使用codeigniter作为前端mysql作为后端创建了一个传销项目 在这里,我怀疑是否有儿童计数 这是我的表格流程: 这是我的树流: 这是我的查询如何获得多级营销(树)子计数 例如: 主用户有14个子用户 我尝试了以下代码: 视图: 型号: public function getProduct2Users($top_id) { $count = 0; $this->db->select('id'); $this->
public function getProduct2Users($top_id)
{
$count = 0;
$this->db->select('id');
$this->db->from('member');
$this->db->where('sponsor', $top_id);
$first_child = $this->db->get();
foreach($first_child->result_array() as $f_child)
{
$f_child_id[] = $f_child['id'];
if(isset($f_child_id))
{
$this->db->select('id');
$this->db->from('member');
$this->db->where('sponsor', $f_child_id);
$second_child = $this->db->get();
}
}
echo'<pre>';print_r($second_child->result_array());exit;
return $first_child->result_array();
}
公共函数getProduct2Users($top\u id)
{
$count=0;
$this->db->select('id');
$this->db->from('member');
$this->db->where('赞助商',$top\u id);
$first_child=$this->db->get();
foreach($first\u child->result\u array()作为$f\u child)
{
$f_child_id[]=$f_child['id'];
if(isset($f_child_id))
{
$this->db->select('id');
$this->db->from('member');
$this->db->where(‘赞助商’,$f_child_id);
$second_child=$this->db->get();
}
}
回显“”;打印($second\u child->result\u array());退出;
返回$first_child->result_array();
}
如果命名约定与树结构相同,请尝试此操作
public function getCountChilds($id)
{
$count = 0;
$query = $this->db
->select('id')
->from('member')
->where('sponsor', $id)
->get();
if ($query->num_rows() > 0)
{
foreach($query->result() AS $objChild)
{
$count += $this->getCountChilds($objChild->id);
++ $count;
}
}
return $count;
}
如果命名约定与树结构相同,请尝试此操作
public function getCountChilds($id)
{
$count = 0;
$query = $this->db
->select('id')
->from('member')
->where('sponsor', $id)
->get();
if ($query->num_rows() > 0)
{
foreach($query->result() AS $objChild)
{
$count += $this->getCountChilds($objChild->id);
++ $count;
}
}
return $count;
}
如果你真的想要一个解决方案,你必须以递归的方式来做 像下面这样的方法应该可以奏效
如果你真的想要一个解决方案,你必须以递归的方式来做 像下面这样的方法应该可以奏效
最好修改您的模式。如果没有大量的代码,这是行不通的。我建议使用一个单独的表/带有fk to user id的表来存储包含主父子孙列的树数据。那么生产这棵树就容易多了。我可能会在db stack网站上寻求有关结构的帮助。@Alex:我只想收集孩子的总数。那么,你能指导我如何为这个流程编写
mysql查询吗?这张桌子有多少顾客?您可能应该向表中添加一个具有此计数的列,并且每次获得新客户时,您都应该更新它,因为性能原因……最好修改您的模式。如果没有大量的代码,这是行不通的。我建议使用一个单独的表/带有fk to user id的表来存储包含主父子孙列的树数据。那么生产这棵树就容易多了。我可能会在db stack网站上寻求有关结构的帮助。@Alex:我只想收集孩子的总数。那么,你能指导我如何为这个流程编写mysql查询吗?这张桌子有多少顾客?您可能应该在表中添加一个包含此计数的列—每次新客户被收购时,您都应该更新它—因为性能原因……感谢很好—但它是否帮助您—它是否解决了您的问题?如果是这样,请接受我的回答,我的意思是你问了一个问题,除了表示感谢之外,你没有以任何方式回应我的评论;)实际上,刚才我正在实现你的代码,它正在工作。非常感谢你。如果你喜欢我的问题,就把它投上去。谢谢很好,但是它帮助你了吗?它解决了你的问题了吗?如果是这样,请接受我的回答,我的意思是你问了一个问题,除了表示感谢之外,你没有以任何方式回应我的评论;)实际上,刚才我正在实现你的代码,它正在工作。非常感谢你。如果你喜欢我的问题,就投票吧。
public function getCountChilds($id)
{
$count = 0;
$query = $this->db
->select('id')
->from('member')
->where('sponsor', $id)
->get();
if ($query->num_rows() > 0)
{
foreach($query->result() AS $objChild)
{
$count += $this->getCountChilds($objChild->id);
++ $count;
}
}
return $count;
}