gpt4 book ai didi

php - 按三列分组在 CodeIgniter 中不起作用

转载 作者:可可西里 更新时间:2023-11-01 06:41:03 24 4
gpt4 key购买 nike

我需要一个包含 Group By 三列的结果,SQL 可以完美运行,没有任何问题。我在 CodeIgniter 框架中使用它时遇到了这个问题;它不执行查询。我的代码和SQL如下。

代码

$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;

当 group_by 只有两列时,这很好用,比如,

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

可能是什么原因?

最佳答案

In codeigniter group_by() works with two fields only.


$this->db->group_by()

允许您编写查询的GROUP BY部分:

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

CI 3 group_by()


编辑 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;

关于php - 按三列分组在 CodeIgniter 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34012566/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com