CodeIgniter Forums
Help with long SQL query - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Help with long SQL query (/thread-31576.html)



Help with long SQL query - El Forum - 06-24-2010

[eluser]ChaosKnight[/eluser]
Hi, this query is for a poll system on the site that I remade:
Code:
$sql = "SELECT v.answer_id, COUNT(v.id) AS NumVotes, a.answer FROM poll_votes v, poll_answers a WHERE v.poll_id='".$poll_id."' AND a.id = v.answer_id GROUP BY a.answer ORDER BY NumVotes DESC";
Can someone tell me if it's possible to convert this query in CodeIgniter's ActiveRecord queries? Or will it be better to run that query with the regular query function?:
Code:
$this->db->query($sql);

Thanks


Help with long SQL query - El Forum - 06-24-2010

[eluser]WanWizard[/eluser]
Something like:
Code:
$this->db->select('v.answer_id, a.answer');
$this->db->select('COUNT(v.id) AS NumVotes', FALSE);
$this->db->join('poll_answers a', 'a.id = v.answer_id');
$this->db->where('v.poll_id', $poll_id);
$this->db->group_by('a.answer');
$this->db->order_by('NumVotes', 'DESC');
$query = $this->db->get('poll_votes v');

Shouldn't be to difficult, after reading the AR section in the manual...


Help with long SQL query - El Forum - 06-24-2010

[eluser]ChaosKnight[/eluser]
Thanks I appreciate your help :-)