我正在尝试获取最近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 no 4 ...
答案 0 :(得分:1)
您的代码无效的原因是您尝试比较两种不同的日期格式。 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 :(得分:0)
试试这个
$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;
}