php - Codeigniter:查询结果不重复字段
问题描述
我的模型中有这个查询:
public function getById(int $id): ?array
{
$query = $this->db->select(['id', 'docent_id', 'date', 'start_at', 'end_at'])
->where('docent_id', $id)
->where('delete_date is null')
->get('Agenda');
$res = $query->result();
return $res;
}
我的问题在我的数据库中,我有类似的东西:
| id | docent_id | date | start_at | end_at
| 1 | 1 | 2020-05-04 | 09:00 | 10:00
| 2 | 1 | 2020-05-04 | 10:00 | 11:00
所以这个结果打印了我相同的 docent_id 两个字段。我会将答案分组,以便日期不会重复两次,这样它就可以为同一个讲解员提供一个相同的结果,具有相同的日期和不同的开始和结束时间。
我能怎么做?谢谢
解决方案
我想你需要的是GROUP BY
. docent_id
以下查询仅在所有 4( 、date
和) 的组合不start_at
同时才会显示数据end_at
。看看这是否对您有帮助。
$query = $this->db->select(['id', 'docent_id', 'date', 'start_at', 'end_at'])
->where('docent_id', $id)
->where('delete_date is null')
->group_by(array("docent_id", "date", "start_at", "end_at"))
->get('Agenda');
$res = $query->result();
推荐阅读
- python - 预测失败:检查输入时出错:预期dense_input 具有形状(2898,)但得到的数组具有形状(1,)
- powershell - PowerShell $_ 语法
- r - 如何让 R 识别来自目录中另一个依赖项的函数?
- hybris - 如何将我的自定义 productcockpit 扩展添加为 hybris 后台透视列表主插槽中的选项之一
- mongodb - 在 MongoDB 中检索嵌套数组中的数据并分页聚合显示所有文档
- ionic5 - Ionic 5:在大屏幕上将页面拆分为两个视图
- excel - VBA - 如果列包含文本并且另一列是工作日,则应用条件格式
- winforms - Winforms 标签的意外行为
- git - 使用 Outlook 电子邮件时 git send-email 出现“发送邮件需要 STARTTLS”错误
- google-maps - 是否可以构建从 lat/lng 位置到地点 ID 的 Google 地图 url?