我正在 codeigniter 工作,我有一个方案表,我的问题是我想显示每个方案的最后更新数据。我不知道该怎么做,请帮忙。
我的表
scheme_code updated_on scheme_name
1 2015-04-13 One
3 2015-04-12 Three
4 2015-04-13 Four
3 2015-04-10 Three
3 2015-04-8 Three
1 2015-04-10 One
4 2015-04-11 Four
我的模型
function show_last_updated_data(){
$this->db->select('*');
$this->db->from('my_table');
$this->db->order_by('updated_on', 'DESC');
$query = $this->db->get();
return $query->result();
}
需要输出
scheme_code updated_on scheme_name
1 2015-04-13 One
3 2015-04-12 Three
4 2015-04-13 Four
最佳答案
我的回答基于@Syed Qarib。我将其修改为与 codeigniter 事件记录格式兼容。
function show_last_updated_data() {
$this->db->select('*, str_to_date(updated_on, "%Y-%M-%d") As date', false); // false to skip escape
$this->db->from('scheme');
$this->db->group_by('scheme_code');
$this->db->order_by('date', 'DESC');
$query = $this->db->get();
return $query->result();
}
编辑
换句话来说,
function show_last_updated_data() {
$max = $this->db->select_max("updated_on")->group_by("scheme_code")->get('scheme')->result_array();
$updated_on = array();
if (count($max)) {
$updated_on = array_column($max, "updated_on"); // make sure your php version is >= 5.5
}
$this->db->select("*");
$this->db->from("scheme");
$this->db->where_in("updated_on", $updated_on);
$query = $this->db->get();
return $query->result();
}
希望对您有用。
关于php - 获取最后更新的数据 - Codeigniter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29620542/