简体   繁体   English

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

[英]How to select by MAX date in CI Active Record

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. created_date字段为mysql_date_time。

I want to get the latest row from this table (based on the created_date field). 我想从该表中获取最新行(基于created_date字段)。 How to do it with CI Active record? 如何使用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() 使用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 https://www.codeigniter.com/userguide3/database/query_builder.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM