php - 通过在 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 时不起作用。那么,我该怎么做呢?请帮我。
谢谢你
解决方案
使用 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');
推荐阅读
- influxdb - 如何使用 lineprotocol 和 API 在 Influx 中插入多个点
- python - 在浏览器中查看 Matplotlib funcAnimation 实时绘图
- android-studio - Android Studio 在想要打开布局时关闭
- javascript - 用文件上传数据
- snowflake-cloud-data-platform - 用于更改数据库或架构中所有表的雪花 SQL 过程
- python - 如何突出显示 Gtk 中的按钮?
- python - 我的 webapp 在 localhost 上运行良好,但在 Heroku 上运行良好
- javascript - JavaScript canvas ball-bounce 算法与绘制函数优化
- delphi - 如何在 Delphi Debugger Visualizer 中获取对对象的引用?
- github - 使用 Github Actions 发送电子邮件