2014-03-28 90 views
0

喜逢一個有你關於子查詢的任何想法,我n codeigniter這裏是我的查詢,我想將其轉換成笨與笨子查詢

SELECT question . * 
FROM question 
WHERE question.id NOT 
       IN (SELECT id 
        FROM answers) 
+4

http://stackoverflow.com/questions/6047149/subquery-in-codeigniter-active-record –

回答

0

首先確保您有問題和答案,因爲你之間的正確關係與回答表的ID匹配問題ID的混亂,也可以擺脫你的子查詢,並可按加入

$this->db->select('q.*'); 
$this->db->from('question q'); 
$this->db->join('answers a','q.id=a.id','LEFT');/* make sure second parameter should match the question id from answer table */ 
$this->db->where('a.id IS NULL',null,FALSE); 
$query = $this->db->get(); 

Active Record

0

子查詢可以放在一個字符串where子句中。

$this->db->select("question.*"); 
$this->db->where("question.id NOT IN (SELECT id FROM answers)"); 
$this->db->get('question'); 
0

$ this-> db-> select('question。*');

$ this-> db-> from('question');

$ this-> db-> where_not_in('question.id','(SELECT id FROM answers)',false);

$ result = $ this-> db-> get() - > result_array();

print_r($ result);

+0

你好,歡迎來到StackOverflow!你能解釋你的主張嗎?此外,在每行代碼的開頭添加4個空格,以使該代碼在此網站上變得更漂亮! (> [edit](https://stackoverflow.com/posts/48337998/edit))謝謝! – NatNgs