[英]Codeigniter: Query result without repetition for a field
我的 model 中有這個查詢:
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
。 只有當所有 4( docent_id
、 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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.