使用CodeIgniter Active Record计算子行不会返回所有结果

时间:2014-05-09 09:15:03

标签: mysql codeigniter activerecord

我的数据库中有一个位置表。地点可能在表格中有父母或子女。见下表:

location_id | location_parent_id | location_name | location_level
------------+--------------------+---------------+---------------
 1          | 0                  | South Africa  | 0
------------+--------------------+---------------+---------------
 2          | 1                  | Gauteng       | 1
------------+--------------------+---------------+---------------
 3          | 1                  | Western Cape  | 1
------------+--------------------+---------------+---------------
 4          | 2                  | Johannesburg  | 2
------------+--------------------+---------------+---------------
 5          | 4                  | Sandton       | 3
------------+--------------------+---------------+---------------
 6          | 4                  | Hilbrow       | 3

我正在使用Codeigniter的活动记录类,并希望选择所有位置。每行应该具有位置的父名称和位置具有的子项数。我已经成功地使用此代码获得了我正在寻找的东西:

$locations = $this->db->select('count(C.location_parent_id) as children_count ,L.location_id, L.location_name, L.location_status, L.location_level, P.location_name as parent_name')
            ->from('locations L')
            ->join('locations P', 'L.location_parent_id=P.location_id','left')
            ->join('locations C', 'C.location_parent_id=L.location_id')
            ->group_by('C.location_parent_id')
            ->get(); 

输出:

ID | NAME         | PARENT        | CHILDREN COUNT| LEVEL
---+--------------+---------------+---------------+------
 1 | South Africa |               | 2             | 0
---+--------------+---------------+---------------+------
 2 | Gauteng      | South Africa  | 2             | 1
---+--------------+---------------+---------------+------
 3 | Johannesburg | Gauteng       | 2             | 2
---+--------------+---------------+---------------+------

所以看起来我只会得到有孩子的行,但我需要那些没有孩子的行。关于我所缺少的任何想法会很棒吗?

编辑期待:

ID | NAME               | PARENT        | CHILDREN COUNT | LEVEL
---+--------------------+---------------+----------------+-------
 1 | South Africa       |               | 2              | 0
---+--------------------+---------------+----------------+-------
 2 | Gauteng            | South Africa  | 1              | 1
---+--------------------+---------------+----------------+-------
 3 | Western Cape       | South Africa  | 0              | 1
---+--------------------+---------------+----------------+-------
 4 | Johannesburg       | Gauteng       | 2              | 2
---+--------------------+---------------+----------------+-------
 5 | Sandton            | Johannesburg  | 0              | 3
---+--------------------+---------------+----------------+-------
 6 | Hilbrow            | Johannesburg  | 0              | 3

1 个答案:

答案 0 :(得分:1)

好吧我明白了。我的 group_by 声明错误,我需要 LEFT 加入孩子,新声明是

$locations = $this->db->select('count(C.location_parent_id) as children_count ,L.location_id, L.location_name, L.location_status, L.location_level, P.location_name as parent_name')
        ->from('locations L')
        ->join('locations P', 'L.location_parent_id=P.location_id','left')
        ->join('locations C', 'C.location_parent_id=L.location_id','left')
        ->group_by('L.location_id')
        ->get();