簡體   English   中英

在codeIgniter查詢中按DESC和'header_id'分組依據的日期順序

[英]Date Order By DESC and 'header_id ' Group By in codeIgniter Query

我需要一個查詢, receive_date降序也有來自表消息的header_id group by。 其中join 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順序。 有些壞了。 一些正確的方法。

$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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM