3 回答

TA貢獻(xiàn)1788條經(jīng)驗(yàn) 獲得超4個贊
->where()
支持將任何字符串傳遞給它,它將在查詢中使用它。
你可以試試這個:
$this->db->select('*')->from('certs');$this->db->where('`id` NOT IN (SELECT `id_cer` FROM `revokace`)', NULL, FALSE);
在,NULL,FALSE
在where()
告訴笨不要逃避查詢,這可能搞砸了。
更新:您還可以查看我編寫的子查詢庫。
$this->db->select('*')->from('certs');$sub = $this->subquery->start_subquery('where_in');$sub->select('id_cer')->from('revokace');$this->subquery->end_subquery('id', FALSE);

TA貢獻(xiàn)1808條經(jīng)驗(yàn) 獲得超4個贊
功能_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);

TA貢獻(xiàn)2012條經(jīng)驗(yàn) 獲得超12個贊
CodeIgniter Active Records當(dāng)前不支持子查詢,但我使用以下方法:
#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()是兩個未記錄的(AFAIK)方法,它們編譯查詢并返回sql(不運(yùn)行它)并重置查詢。
在主查詢中,where子句中的FALSE告訴codeigniter不要轉(zhuǎn)義查詢(或添加反引號等),這會弄亂查詢。(NULL只是因?yàn)閣here子句有一個我們沒有使用的可選第二個參數(shù))
但是,您應(yīng)該知道_compile_select()和_reset_select()不是文檔化的方法,在將來的版本中可能會出現(xiàn)功能(或存在)的變化。
添加回答
舉報