在codeIgniter查询中按DESC和'header_id'分组依据的日期顺序

时间:2018-09-25 09:58:29

标签: php mysql mysqli codeigniter-3

我需要一个查询,receive_date降序也有来自表消息的header_id分组依据。将message_headersstatus = 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顺序。有些坏了。一些正确的方法。

2 个答案:

答案 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();