【问题标题】:In mysql Get the count of left and right child of root user在mysql中获取root用户左右孩子的个数
【发布时间】:2020-06-15 20:50:10
【问题描述】:

有什么方法可以通过其父 id 获取左右子节点的总数,直到子级别的“N”个。

这是我的用户表,我在其中存储带有腿(左/右)位置的父子信息

地点:

  1. referral_id:是子用户的父用户id
  2. left_child_id: 是左腿加入用户的子用户id
  3. right_child_id: 是右腿加入用户的子用户的id。
  4. position_to_referral: 是他加入父用户的位置名称(左/右腿)

欢迎任何帮助或任何建议。

我可以使用下面的 php 代码获取计数,但我想直接从 mysql 获取计数

function countChildren($parentId, $Nlevel, $tempLevel = 0)
{
    if ($tempLevel < $Nlevel) {
        $tempLevel = $tempLevel + 1;
        $children = User::where('referral_id', $parentId)->get()->pluck('id');
        $count = count($children);
        foreach ($children as $userId) {
            $count += $this->countChildren($userId, $Nlevel, $tempLevel);
        }
        return $count;
    }
}

编辑 2

更新了代码以在有或没有 N 级的情况下向左、向右

public function countChildren($parentId, $Nlevel = 0, $legPosition = 0, $tempLevel = 0)
{
    if ($Nlevel) {
        if ($tempLevel < $Nlevel) {
            $tempLevel = $tempLevel + 1;
            if ($tempLevel == 1 && $legPosition) {
                $children = User::where('referral_id', $parentId)
                    ->where('position_to_referral', $legPosition)
                    ->get()->pluck('id');
            } else {
                $children = User::where('referral_id', $parentId)->get()->pluck('id');
            }
            $count = count($children);
            foreach ($children as $userId) {
                $count += $this->countChildren($userId, $Nlevel, $legPosition, $tempLevel);
            }
            return $count;
        }
    } else {
        if ($legPosition) {
            $children = User::where('referral_id', $parentId)
                ->where('position_to_referral', $legPosition)
                ->get()->pluck('id');
        } else {
            $children = User::where('referral_id', $parentId)->get()->pluck('id');
        }
        $count = count($children);
        foreach ($children as $userId) {
            $count += $this->countChildren($userId);
        }
        return $count;
    }
}

调用上面的函数来获取计数:

$total = $profileService->countChildren(1); // get total children count of user_id = 1
$totalWith3Level = $profileService->countChildren(1, 3); // get total children count till 3 level
$totalLeft = $profileService->countChildren(1, '', 'left'); // get total children count of left leg
$totalRight = $profileService->countChildren(1, '', 'right'); // get total children count of right leg
$totalLeftWith3Level = $profileService->countChildren(1, 3, 'left'); // get total children count of left leg till 3 level
$totalRightWith3Level = $profileService->countChildren(1, 3, 'right'); // get total children count of right leg till 3 level

【问题讨论】:

  • 请添加例外的输出以及您到目前为止所尝试的内容。
  • @AkhileshMishra 我用尝试过的解决方案更新了我的问题
  • 请同时发布预期输出

标签: php mysql laravel binary-tree


【解决方案1】:

嵌套集合模型不是更有意义吗?

            1 A 14
              |
      +-------+--------+
      |                |
    2 B 7            8 C 13
      |                |
  +---+---+       +----+----+
  |       |       |         |
3 D 4   5 E 6   9 F 10   11 G 12


user lft rgt
A      1  14
B      2   7
C      8  13
D      3   4
E      5   6
F      9  10
G     11  12

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-24
    • 1970-01-01
    • 2012-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-11
    • 1970-01-01
    相关资源
    最近更新 更多