使用codeigniter进行子查询

时间:2014-03-28 22:27:13

标签: php mysql codeigniter

嗨,每个人都对子查询有任何想法我n codeigniter这里是我的查询我想将其转换为codeigniter

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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

子查询可以放在字符串where子句中。

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

答案 2 :(得分:0)

$这 - > DB->选择( '*问题。');

$这 - > DB-肽从( '问题');

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

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

的print_r($结果);