2014-05-09 1 views
0

데이터베이스에 위치 테이블이 있습니다. 위치에 부모님이나 자녀가있을 수 있습니다. 아래 표를 참조하십시오 :CodeIgniter Active Record로 하위 행을 계산하면 모든 결과가 반환되지 않습니다.

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 
---+--------------+---------------+---------------+------ 

이 그래서 난 단지이 chidren을 가지고 행을 얻고 있음을 나타납니다하지만 난 필요 그 행은 자식도 없다. 내가 누락 된 것에 대한 아이디어는 훌륭 할까? 기대

편집는 :

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 
+0

입니다, 아이들에 가입 내 GROUP_BY 문은 잘못이고 나는 LEFT에 필요한? –

+0

@M Khalid Junaid - 예상 리콜로 편집을 추가했습니다. –

답변

1

좋아, 내가 그것을 알아 냈어. 새로운 사항이이 결과 세트 것으로 예상된다 무엇

$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(); 
관련 문제