codeigniter 获取最后 1 分钟的数据库字段
codeigniter get database field of last 1 minute
我正在尝试获取最后 1 分钟的数据库结果。数据库如下所示:
id| from | to | username | status | dbtime
-------------------------------------------------------
1 | u_1 | v_2 | myname | 0 | 2015-02-19 14:30:0
2 | u_1 | v_2 | myname1 | 1 | 2015-02-19 14:30:0
3 | u_3 | v_1 | myname2 | 0 | 2015-02-19 16:30:0
4 | u_1 | v_2 | myname3 | 1 | 2015-02-19 16:30:0
5 | u_1 | v_2 | myname4 | 0 | 2015-02-19 16:30:0
假设当前时间是 2015-02-19 16:30:0 ..我的目标是从数据库中获取 ID 号 4,因为它的当前时间和状态是 1 ..这是我的查询:
$this->db->select();
$this->db->order_by('dbtime', 'asc');
$result = $this->db->get_where('dbname', array('to' => $someid, 'status' => 1, 'dbtime >=' => time()- 90, ),1 );
if ($result->num_rows() > 0)
{
return 1;
}
else {
return 0;
}
但是这个查询没有给我预期的结果。它返回数据库列 id 2 而不是 id 4 ...
试试这个
$this->db->select();
$this->db->order_by('dbtime', 'asc');
$result = $this->db->get_where('dbname', array('to' => $someid, 'status' => 1, 'dbtime' => date_sub(now(), interval 1 minute)),1 );
if ($result->num_rows() > 0)
{
return 1;
}
else {
return 0;
}
您的代码不起作用的原因是您试图比较两种不同的日期格式。 Mysql 需要查看与存储的内容等效的数据,以便 运行 进行比较。这就是我要做的:
function getData($someid) {
$curr = date('Y-m-d H:i:s');
$last_min = date('Y-m-d H:i:s', strtotime('-1 minutes'));
$this->db->select();
$this->db->from('dbname');
$this->db->where('to', $someid);
$this->db->where('status', 1);
$this->db->where('dbtime >=', $last_min);
$this->db->where('dbtime <=', $curr);
$this->db->order_by('dbtime', 'asc');
$result = $this->db->get();
if ($result->num_rows() > 0) {
return 1;
} else {
return 0;
}
}
我正在尝试获取最后 1 分钟的数据库结果。数据库如下所示:
id| from | to | username | status | dbtime
-------------------------------------------------------
1 | u_1 | v_2 | myname | 0 | 2015-02-19 14:30:0
2 | u_1 | v_2 | myname1 | 1 | 2015-02-19 14:30:0
3 | u_3 | v_1 | myname2 | 0 | 2015-02-19 16:30:0
4 | u_1 | v_2 | myname3 | 1 | 2015-02-19 16:30:0
5 | u_1 | v_2 | myname4 | 0 | 2015-02-19 16:30:0
假设当前时间是 2015-02-19 16:30:0 ..我的目标是从数据库中获取 ID 号 4,因为它的当前时间和状态是 1 ..这是我的查询:
$this->db->select();
$this->db->order_by('dbtime', 'asc');
$result = $this->db->get_where('dbname', array('to' => $someid, 'status' => 1, 'dbtime >=' => time()- 90, ),1 );
if ($result->num_rows() > 0)
{
return 1;
}
else {
return 0;
}
但是这个查询没有给我预期的结果。它返回数据库列 id 2 而不是 id 4 ...
试试这个
$this->db->select();
$this->db->order_by('dbtime', 'asc');
$result = $this->db->get_where('dbname', array('to' => $someid, 'status' => 1, 'dbtime' => date_sub(now(), interval 1 minute)),1 );
if ($result->num_rows() > 0)
{
return 1;
}
else {
return 0;
}
您的代码不起作用的原因是您试图比较两种不同的日期格式。 Mysql 需要查看与存储的内容等效的数据,以便 运行 进行比较。这就是我要做的:
function getData($someid) {
$curr = date('Y-m-d H:i:s');
$last_min = date('Y-m-d H:i:s', strtotime('-1 minutes'));
$this->db->select();
$this->db->from('dbname');
$this->db->where('to', $someid);
$this->db->where('status', 1);
$this->db->where('dbtime >=', $last_min);
$this->db->where('dbtime <=', $curr);
$this->db->order_by('dbtime', 'asc');
$result = $this->db->get();
if ($result->num_rows() > 0) {
return 1;
} else {
return 0;
}
}