2011-12-31 3 views
0

어떻게하면 다음 쿼리를 활성 레코드에 codeigniter에 쓸 수 있습니까? tbl_list 에서Codeigniter 하위 쿼리 조인

선택 tbl_list.id, tbl_list_items.name 는 = tbl_list_items.id에 tbl_list_items 가입 (tbl_list_items에서 선택 tbl_list_items.id 여기서 tbl_list_items.item_no 오름차순 LIMIT list_id로하여 1 = tbl_list.id 순서)

업데이트 :

이것은 내가 출현 한 것이지만 일을하지 않습니다.

$this->db->select($this->table_list.'.*'); 
$this->db->from($this->table_list); 
$sub = $this->subquery->start_subquery('join','',$this->table_list_items.'.id'); 
$sub->select('id')->from($this->table_list_items)->where('list_id','tbl_list.id')->order_by($this->table_list_items.'item_no','asc')->limit('1'); 
$this->subquery->end_subquery('list_items'); 

감사합니다. 감사.

+0

마지막 선택 tbl_list.id로 문을 변경 tbl_list 에서 tbl_list_items.name는 = (tbl_list_items에서 선택 tbl_list_items.id 여기서 item_no 오름차순 LIMIT 목록 ID = 1에 의해 tbl_list.id 순서)에 tbl_list_items.id tbl_list_items 가입; 활성 레코드가 더 행복해 보입니다. –

+0

항상 Active SQL을 직선 SQL 문과 함께 사용할 수 있습니다. 그러나 그때 당신은 성명을 교환 가능한 구성 요소로 분해하는 것을 활용하지 않습니다. –

답변

1

Review the Active Record methods available to you in CodeIgniter.

Then review a CI library to extend it to handle subqueries available on CodeIgniter

There is a walk-through about how CI is doing this under-the-hood

Found a github codebase for implementing subqueries in CodeIgniter 1.7 and 2.x (see link)

편집 :

개정 코드 아시스에 다음과 같습니다 티. 또한 네 번째 링크의 예를 확인할 수도 있습니다.

는 you'er는, 코드가 유사 할 것이다 일을하려고 무엇을 복제 (이 ... 않은 시험이지만 희망 옳은 길을 시작)하려면

$this->db->select('tbl_list.id, tbl_list.names'); 
$this->db->from('tbl_list'); 
$sub = $this->subquery->start_subquery('join', 'left', 'tli.id = tbl_list.id'); 
$sub->select('tbl_list_items.id')->from('tbl_list_items')->where('tbl_list_items.list_id=tbl_list.id '); 
$sub->db->order_by('ASC'); 
$sub->db->limit(1); 
$this->subquery->end_subquery('tli'); 

의 핵심은 이것입니다 문 : $ this-> subquery-> end_subquery ('tli'); 그 결과는 AS tli이라는 하위 쿼리 결과를 tli.id에서 참조 할 수 있도록 처리한다는 것입니다.

희망이 도움이되었습니다!

+0

세 번째 줄을 찾지 못했습니다. tbl_list_items.id는 tbl_list.id와 같지 않습니다. 나는 문서를 읽었지만 조인을 오른쪽으로 하위 쿼리하는 방법을 보여주지 않습니다. 어떤 조언. –

+0

세 번째 줄에 다음과 같이 입력하십시오 : $ sub = $ this-> subquery-> start_subquery ('join', tbl_list_items.id = list_ids '); 나머지 쿼리의 경우 '... AS'list_ids '를 사용하는 SELECT 문을 만듭니다. 나는 오늘 나중에 그것을 쓸 것이다. –

+0

CI의 AR에 대한 하위 쿼리 라이브러리 및 설명이 포함 된 새 코드베이스에 대한 링크로 업데이트되었습니다. –