首页 > 解决方案 > 通过在 codeigniter 中不使用 Group by 进行排序

问题描述

$loginuserID = $this->session->userdata("loginuserID");

$this->db->select('notice_x_user.*,notice.*,classes.*,sub_courses.*,,notice.status as studentStatus,notice.date as noticeDate');
$this->db->from('notice_x_user');
$this->db->join('notice','notice_x_user.noticeID = notice.noticeID', 'LEFT');
$this->db->join('classes', 'classes.ClassesID = notice.classesID', 'LEFT');
$this->db->join('sub_courses', 'sub_courses.sub_coursesID = notice.sub_coursesID', 'LEFT'); 
$wheres = "(notice_x_user.userID = '".$loginuserID."' and notice_x_user.usertype = 'Support') or notice.userID = '".$loginuserID."'";
$this->db->where($wheres);
$this->db->order_by('notice.noticeID', 'DESC'); 
$this->db->group_by('notice.noticeID');

我加入了四个表,在那里我得到了正确的数据,但问题是 order by 在使用 group by 时不起作用。那么,我该怎么做呢?请帮我。

谢谢你

标签: phpmysqlcodeigniter-3

解决方案


使用 select_max 获取最大通知 ID

$this->db->select('notice_x_user.*,notice.*,classes.*,sub_courses.*,,notice.status as studentStatus,notice.date as noticeDate');
$this->db->select_max('notice.noticeID' , 'noticeID');
$this->db->from('notice_x_user');
$this->db->join('notice','notice_x_user.noticeID = notice.noticeID', 'LEFT');
$this->db->join('classes', 'classes.ClassesID = notice.classesID', 'LEFT');
$this->db->join('sub_courses', 'sub_courses.sub_coursesID = notice.sub_coursesID', 'LEFT'); 
$wheres = "(notice_x_user.userID = '".$loginuserID."' and notice_x_user.usertype = 'Support') or notice.userID = '".$loginuserID."'";
$this->db->where($wheres);
$this->db->order_by('noticeID', 'DESC'); 
$this->db->group_by('notice.noticeID');

推荐阅读