如何在CI Active Record中按MAX日期选择

Hi my table structure is given below

login_session_id, user_id, created_date, ci_cession_id, user_agent_string

The created_date field is mysql_date_time.

I want to get the latest row from this table (based on the created_date field). How to do it with CI Active record?

Try this:

$this->db->select('*');
$this->db->from('** YOUR TABLE HERE **');
$this->db->order_by('created_date', 'desc');
$this->db->limit(1);
$query = $this->db->get();

This should work by selecting all columns from the table (which you'll need to specify), ordering all rows with the most recent date at the top, then limiting it to the top row only which will be the most recent entry.

use order_by()

 $this->db->select('login_session_id, user_id, created_date, ci_cession_id, user_agent_string');
 $this->db->from("table_name");
 $this->db->order_by("created_date", "desc");
 $query = $this->db->get(); 
 return $query->result();

this will do the trick

For more take a reference from HERE

the best way is

$this->db->select_max('date');
$query = $this->db->get('members');  // Produces: SELECT MAX(date) as date FROM members
return $query->result();

https://www.codeigniter.com/userguide3/database/query_builder.html