【发布时间】:2016-06-03 08:24:43
【问题描述】:
我想从数据库中获取最近的记录,限制为 5。我使用了下面的代码但没有工作。也许我错过了什么或者整个查询是错误的。请指导我。谢谢
型号
public function get_comments ($id) {
$this->db->select('*');
$this->db->order_by('id', 'ASC');
$this->db->from('Item_comments');
$this->db->limit('5');
$this->db->where(array('checklist_item_id' => $id, 'status' => 1));
$query = $this->db->get();
return $query->result_array();
}
注意:查询工作正常并从 db 获取 5 条记录,但我想 将最近的位置放在底部。谢谢
【问题讨论】:
-
如何但不工作?
-
@splash58,我更新了简介。它正在工作,但我想在底部显示最近的记录
-
您需要使用子查询
select *from (your_query) t order by id desc。你能在 CI 中写子查询吗? -
我最近写的 - stackoverflow.com/questions/37360521/… 不一样,但是例子
-
你为什么不使用
return array_reverse($query->result_array());,因为这肯定会解决你的问题。
标签: php codeigniter