7

I needed a result with Group By three columns and the SQL works perfectly without any issue. I'm facing the issue when I use it in the CodeIgniter framework; it doesn't execute the query. My Code and SQL are as follows.

Code

$this->db->select(['trk.userid AS user_id', 'scr.course AS course_id'])
                     ->from('mdl_scorm scr')
                     ->join('mdl_scorm_scoes_track trk', 'scr.id = trk.scormid', 'inner')
                     ->join('mdl_course_modules mcs', 'mcs.instance = scr.id', 'inner')
                     ->where_in('trk.value', ['completed','incomplete','passed'])
                     ->group_by(['scr.course', 'trk.userid', 'trk.scormid'])
                     ->order_by('trk.userid', 'DESC');

SQL

SELECT `trk`.`userid` AS user_id, `scr`.`course` AS course_id 
FROM (`mdl_scorm` scr) 
INNER JOIN `mdl_scorm_scoes_track` trk ON `scr`.`id` = `trk`.`scormid` INNER JOIN `mdl_course_modules` mcs ON `mcs`.`instance` = `scr`.`id` 
WHERE `trk`.`value` IN ('completed', 'incomplete', 'passed') 
GROUP BY `scr`.`course`, `trk`.`userid`, `trk`.`scormid` 
ORDER BY `trk`.`userid` DESC LIMIT 0,100;

This works fine when the group_by has only two columns, like,

->group_by(['scr.course', 'trk.userid'])

What could be the reason?

4 Answers 4

11

In codeigniter group_by() works with two fields only.


$this->db->group_by()

Permits you to write the GROUP BY portion of your query:

$this->db->group_by(array("title", "date"));  // Produces: GROUP BY title, date

CI 3 group_by()


Edit 01

$query=$this->db->query("
        SELECT `trk`.`userid` AS user_id, `scr`.`course` AS course_id 
        FROM `mdl_scorm` scr 
        INNER JOIN `mdl_scorm_scoes_track` trk 
        ON `scr`.`id` = `trk`.`scormid` 
        INNER JOIN `mdl_course_modules` mcs 
        ON `mcs`.`instance` = `scr`.`id` 
        WHERE `trk`.`value` IN ('completed', 'incomplete', 'passed') 
        GROUP BY `scr`.`course`, `trk`.`userid`, `trk`.`scormid` 
        ORDER BY `trk`.`userid` 
        DESC LIMIT 0,100;");

$result = $query->result_array();
return $result;
7
  • Thanks. Documentation never mentioned on the limitation of field used in group_by(). In the documentation it says You can also pass an array of multiple values as well:
    – Vaishak
    Commented Dec 1, 2015 at 4:45
  • Actually the means only 2. Commented Dec 1, 2015 at 4:45
  • @Vaishak any comments?? Commented Dec 1, 2015 at 5:02
  • I was trying what @Drew had suggested, using raw query. But that doesn't work either. The issue occurs at $result = $query->result(); or even at $result = $query->row_array();
    – Vaishak
    Commented Dec 1, 2015 at 5:19
  • So the issue is not at the level of executing the query, but while retrieving the result, I guess.
    – Vaishak
    Commented Dec 1, 2015 at 5:20
3

Consider a raw query in the model.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class xyz_model extends CI_Model{
     function __construct()
     {
          // Call the Model constructor
          parent::__construct();
     }

     function get_this_thing($month,$year) 
     {    $myQuery="SELECT `trk`.`userid` AS user_id, `scr`.`course` AS course_id 
          FROM `mdl_scorm` scr 
          INNER JOIN `mdl_scorm_scoes_track` trk 
          ON `scr`.`id` = `trk`.`scormid` 
          INNER JOIN `mdl_course_modules` mcs 
          ON `mcs`.`instance` = `scr`.`id` 
          WHERE `trk`.`value` IN ('completed', 'incomplete', 'passed') 
          GROUP BY `scr`.`course`, `trk`.`userid`, `trk`.`scormid` 
          ORDER BY `trk`.`userid` 
          DESC LIMIT 0,100;";

          $query = $this->db->query($myQuery);

          $result = $query->result();
          return $result;
     }
}
3
  • No. This doesn't work either. The code stops at $result = $query->result();
    – Vaishak
    Commented Dec 1, 2015 at 5:17
  • 1
    what table sizing are we talking about. I know queries from some people that run for an hour. That is how I meet them. I am going on the fact that your query works before you come in here talking about 3 columns in the group by, or trying to get a raw to work that otherwise would be tricky or impossible with CI
    – Drew
    Commented Dec 1, 2015 at 5:32
  • It was due to memory limitation, I wasn't able to get the expected result, I guess. I had to tweak the query and memory/cache limit to get expected result.
    – Vaishak
    Commented Dec 1, 2015 at 7:40
1

You can also pass an array of multiple values as well:

$this->db->group_by(array("title", "date"));  // Produces: GROUP BY title, date

Source link

0

use result array instead of the result like

return $query->result_array();

Not the answer you're looking for? Browse other questions tagged or ask your own question.