CodeIgniter Forums
Compare data from two different tables using codeigniter - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Compare data from two different tables using codeigniter (/showthread.php?tid=58204)



Compare data from two different tables using codeigniter - El Forum - 05-23-2013

[eluser]Unknown[/eluser]
I am new to codeigniter and I want to write this mySql query
Code:
SELECT COUNT(DISTINCT source) AS NumberOfEvaluations
FROM COMMENT , EVALUATION
WHERE evaluation_id = EVALUATION.id AND EVALUATION.department_id = '$department_id' ;

into codeigniter.
This is my try

Code:
$this->db->select('t1.count(DISTINCT(source))');  
   $this->db->from('comment t1');
   $this->db->join('evaluation t2', 't2.id = t1.evaluation_id', 'left');    

   $this->db->where();
  
$query=$this->db->get();  
   return $query->num_rows();



Compare data from two different tables using codeigniter - El Forum - 05-23-2013

[eluser]Unknown[/eluser]
Maybe this one ?
Code:
$this->db->select('count(DISTINCT(source))');  
$this->db->from('comment c','evaluation e');  

$this->db->where('c.evaluation_id','e.id');
$this->db->where('e.department_id', $department_id);



Compare data from two different tables using codeigniter - El Forum - 05-24-2013

[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter forums!

You didn't mention what the problem was.

I assume you're having trouble with CodeIgniter escaping your SELECT clause? You can pass FALSE into $this->db->select() as the second parameter to prevent that.
Code:
$this->db->select('count(DISTINCT source)', FALSE);  
$this->db->from('comment c','evaluation e');  

$this->db->where('c.evaluation_id','e.id');
$this->db->where('e.department_id', $department_id);