【问题标题】:Using CodeIgniter framework. How to get_where but limit the results to the past 12 hours?使用 CodeIgniter 框架。如何 get_where 但将结果限制在过去 12 小时内?
【发布时间】:2017-11-30 02:34:48
【问题描述】:

基本上,我有一个专用于用户提交的整个数据库表。每行都有一列timestamp,这是用户提交时的php time(),以及username,我用来将表过滤为特定的用户名。

但用户名有多行(假设超过 9000 行)。

如何才能进一步过滤到过去 12 小时的数据?这是43200 seconds

我怎样才能有效地做到这一点?

我似乎找不到可以通过 BETWEEN nownow - 43200 之类的方式限制结果的 CodeIgniter db query 函数

我必须注意 timestamp 字段的类型为 BIGINT(20) 并且它存储 PHP 时间戳 time()

【问题讨论】:

  • 您能告诉我们您的查询吗?到目前为止,您尝试过什么?
  • @FMashiro - 目前是$query = $this->db->get_where('reports', array('username' => $username));,但不确定将结果有效限制在过去的最佳方法43200 seconds

标签: php mysql codeigniter timestamp between


【解决方案1】:

只需使用 where 子句where("timestamp_column <=",$time);

 $time =time()-43200;
 $this->db->select("colum1,column2");
 $this->db->where("timestamp_column >=",$time);
 $this->db->from("tabl_name");
 $result = $this->db->get()->result();

【讨论】:

  • 不应该是>=吗?
【解决方案2】:

使用下面的查询

SELECT * FROM reports WHERE username = "username" and `Date` > NOW() - INTERVAL 12 HOUR

在代码点火器中

$this->db->query("SELECT * FROM reports WHERE username = "username" and `Date` > NOW() - INTERVAL 12 HOUR")->result_array();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-02
    • 2023-03-05
    • 2019-09-02
    • 1970-01-01
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多