通过不与Codeigniter中的Group by一起订购

时间:2020-01-02 11:49:02

标签: php mysql codeigniter-3

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

我加入了四个表,在其中我可以获取正确的数据,但是问题是在使用分组依据时,按顺序排列不起作用。那么,我该怎么做?请帮助我。

谢谢

3 个答案:

答案 0 :(得分:0)

使用select_max获取最大的noticeID

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

答案 1 :(得分:0)

分组语句必须之前使用,否则会出现MySQL错误。

只需按正确的顺序放置语句:

$this->db->group_by('notice.noticeID');
$this->db->order_by('notice.noticeID', 'DESC'); 

它将按预期工作

答案 2 :(得分:0)

尝试一下

$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->group_by('notice.noticeID');
$this->db->order_by('notice.noticeID', 'DESC');