[英]Date Order By DESC and 'header_id ' Group By in codeIgniter Query
我需要一個查詢, receive_date
降序也有來自表消息的header_id
group by。 其中join message_headers
表status = 0
。
$this->db->select('me.*, mh.*,max(me.receive_date) as receive_date');
$this->db->from('messages as me');
$this->db->join('message_headers as mh', 'em.header_id = eh.id');
$this->db->where('me.account_id',1);
$this->db->where('mh.status', 0);
$this->db->order_by('receive_date', 'DESC');
$this->db->group_by('me.header_id');
$message = $this->db->get()->result_array();
輸出不顯示desc
順序。 有些壞了。 一些正確的方法。
$this->db->select('me.*, mh.*,max(me.receive_date) as receive_date');
$this->db->from('messages as me');
$this->db->join('message_headers as mh', 'me.header_id = mh.id');
$this->db->where('me.account_id',1);
$this->db->where('mh.status', 0);
$this->db->group_by('me.header_id');
$this->db->order_by('receive_date', 'DESC');
$message = $this->db->get()->result_array();
使用STR_TO_DATE
作為日期格式。
$this->db->select('me.*, mh.*, MAX(STR_TO_DATE(me.receive_date, '%Y-%m-%d %h:%i:%s')) as receive_date');
$this->db->from('messages as me');
$this->db->join('message_headers as mh', 'em.header_id = eh.id');
$this->db->where('me.account_id',1);
$this->db->where('mh.status', 0);
$this->db->order_by('receive_date', 'DESC');
$this->db->group_by('me.header_id');
$message = $this->db->get()->result_array();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.