我需要一个查询,receive_date
降序也有来自表消息的header_id
分组依据。将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
顺序。有些坏了。一些正确的方法。
答案 0 :(得分: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', '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();
答案 1 :(得分:0)
使用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();