hi every one have you any idea about subquery in codeigniter
here is my query i want to convert it into codeigniter
SELECT question . *
FROM question
WHERE question.id NOT
IN ( SELECT id
FROM answers )
hi every one have you any idea about subquery in codeigniter
here is my query i want to convert it into codeigniter
SELECT question . *
FROM question
WHERE question.id NOT
IN ( SELECT id
FROM answers )
First make sure you have correct relation between question and answers because you are matching the question id with the id of answer table its confusing,also you can get rid of your subquery and use join instead
$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();
Subqueries can be placed in a string where clause.
$this->db->select("question.*");
$this->db->where("question.id NOT IN (SELECT id FROM answers)");
$this->db->get('question');
$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);