Raj*_*jan 3 php mysql sql codeigniter
我想知道我有数据的最后日期.所以我想在我的列date_data中打印最后一个日期.
在模型中:
public function last_record()
{
$query = $this->db->select('LAST(date_data)');
$this->db->from('date_data');
return $query;
}
Run Code Online (Sandbox Code Playgroud)
在控制器中:
$last_data = $this->attendance_m->last_record();
var_dump($last_data);
Run Code Online (Sandbox Code Playgroud)
但我没有得到理想的结果
试试这个
$query = $this->db->query("SELECT * FROM date_data ORDER BY id DESC LIMIT 1");
$result = $query->result_array();
return $result;
Run Code Online (Sandbox Code Playgroud)
为了从表中获取最后一条记录,请ORDER BY DESC与LIMIT 1
return $last_row=$this->db->select('*')->order_by('id',"desc")->limit(1)->get('date_data')->row();
Run Code Online (Sandbox Code Playgroud)
您可以按照以下代码执行此操作:
public function get_last_record(){
$this->db->select('*');
$this->db->from('date_data');
$this->db->order_by('created_at', 'DESC'); // 'created_at' is the column name of the date on which the record has stored in the database.
return $this->db->get()->row();
}
Run Code Online (Sandbox Code Playgroud)
如果您愿意,您也可以设置限制。