2014-03-28 66 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