codeigniter 활성 레코드의 하위 쿼리
SELECT * FROM certs WHERE id NOT IN (SELECT id_cer FROM revokace);
CodeIgniter 활성 레코드에서 위의 select 문을 어떻게 작성합니까?
->where()
문자열 전달을 지원하고 쿼리에서 사용합니다.
이것을 사용해 볼 수 있습니다.
$this->db->select('*')->from('certs');
$this->db->where('`id` NOT IN (SELECT `id_cer` FROM `revokace`)', NULL, FALSE);
,NULL,FALSE
에서이 where()
할 수있는 엉망 그것을 쿼리를 탈출하지 CodeIgniter를 알려줍니다.
업데이트 : 내가 작성한 하위 쿼리 라이브러리를 확인할 수도 있습니다 .
$this->db->select('*')->from('certs');
$sub = $this->subquery->start_subquery('where_in');
$sub->select('id_cer')->from('revokace');
$this->subquery->end_subquery('id', FALSE);
_compile_select()
및 함수 _reset_select()
는 더 이상 사용되지 않습니다.
대신 다음을 사용하십시오 get_compiled_select()
.
#Create where clause
$this->db->select('id_cer');
$this->db->from('revokace');
$where_clause = $this->db->get_compiled_select();
#Create main query
$this->db->select('*');
$this->db->from('certs');
$this->db->where("`id` NOT IN ($where_clause)", NULL, FALSE);
CodeIgniter Active Records는 현재 하위 쿼리를 지원하지 않지만 다음 접근 방식을 사용합니다.
#Create where clause
$this->db->select('id_cer');
$this->db->from('revokace');
$where_clause = $this->db->_compile_select();
$this->db->_reset_select();
#Create main query
$this->db->select('*');
$this->db->from('certs');
$this->db->where("`id` NOT IN ($where_clause)", NULL, FALSE);
_compile_select () 및 _reset_select ()는 쿼리를 컴파일하고 SQL을 반환하고 (실행하지 않고) 쿼리를 재설정하는 두 가지 문서화되지 않은 (AFAIK) 메서드입니다.
주 쿼리에서 where 절의 FALSE는 codeigniter에게 쿼리를 엉망으로 만드는 쿼리를 이스케이프 (또는 백틱 추가 등)하지 않도록 지시합니다. (NULL은 단순히 where 절에 사용하지 않는 선택적 두 번째 매개 변수가 있기 때문입니다.)
그러나 _compile_select () 및 _reset_select ()는 문서화 된 메서드가 아니므로 향후 릴리스에서 기능 (또는 존재)이 변경 될 수 있음을 알고 있어야합니다.
원래 질문에 대해서는 약간 늦을 수 있지만 향후 질문에는 도움이 될 수 있습니다. 이를 달성하는 가장 좋은 방법은 내부 쿼리의 결과를 다음과 같은 배열로 가져 오는 것입니다.
$this->db->select('id');
$result = $this->db->get('your_table');
return $result->result_array();
그리고 다음 활성 레코드 절에서 배열보다 사용하십시오.
$this->db->where_not_in('id_of_another_table', 'previously_returned_array');
도움이 되었기를 바랍니다
쿼리의 경우 다음 SELECT * FROM (SELECT id, product FROM product) as product
을 사용할 수 있습니다.
$sub_query_from = '(SELECT id, product FROM product ) as product';
$this->db->select();
$this->db->from($sub_query_from);
$query = $this->db->get()
sub_query_from 문자열에서 사이에 공백을 사용해야합니다. ... product ) as...
$this->db->where('`id` IN (SELECT `someId` FROM `anotherTable` WHERE `someCondition`='condition')', NULL, FALSE);
I think this code will work. I dont know if this is acceptable query style in CI but it works perfectly in my previous problem. :)
$subquery = 'SELECT id_cer FROM revokace';
$this->db->select('*');
$this->db->where_not_in(id, $subquery);
$this->db->from('certs');
$query = $this->db->get();
$where.= '(';
$where.= 'admin_trek.trek='."%$search%".' AND ';
$where.= 'admin_trek.state_id='."$search".' OR ';
$where.= 'admin_trek.difficulty='."$search".' OR ';
$where.= 'admin_trek.month='."$search".' AND ';
$where.= 'admin_trek.status = 1)';
$this->db->select('*');
$this->db->from('admin_trek');
$this->db->join('admin_difficulty',admin_difficulty.difficulty_id = admin_trek.difficulty');
$this->db->where($where);
$query = $this->db->get();
참고URL : https://stackoverflow.com/questions/6047149/subquery-in-codeigniter-active-record
'Development Tip' 카테고리의 다른 글
Python에서 읽기 전용 클래스 속성을 만드는 방법은 무엇입니까? (0) | 2020.11.23 |
---|---|
IntelliJ가 "새"표현식에서 생성자 매개 변수를 자동 완성 할 수 있습니까? (0) | 2020.11.23 |
기본 클래스 생성자를 어떻게 호출합니까? (0) | 2020.11.23 |
matplotlib를 사용하여 두 그림을 표시하는 방법은 무엇입니까? (0) | 2020.11.23 |
현재 인터페이스 방향을 얻는 다른 방법? (0) | 2020.11.23 |