php 如何使用活动记录查询指定时间之间日期的sql

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12102195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:49:10  来源:igfitidea点击:

How to query sql with active record for dates between specified times

phpmysqlcodeigniterphpactiverecordactive-record-query

提问by michael

I have a database that I want to pull only certain rows that have dates in specified ranges. I'm not sure how to do this properly in active record. Right now it looks like I'm running a standard mysql query inside of an active record query. I hope this gives you the idea of what I'm looking for.

我有一个数据库,我只想提取日期在指定范围内的某些行。我不确定如何在活动记录中正确执行此操作。现在看起来我正在活动记录查询中运行标准的 mysql 查询。我希望这能让你知道我在寻找什么。

I would also like to be able to get rows with anything before today, including today and 3 days in the future.

我还希望能够在今天之前获得任何行,包括今天和未来 3 天。

$query = $this->db->query("SELECT * FROM 'topics_list' . 'topic date' WHERE DATE(order_datetime) BETWEEN '2012-10-01' AND '2012-10-3'");

回答by Kanishka Panamaldeniya

this is the way . but according to the DATE format you have in the database you have to change 2012-10-01and 2012-10-03

就是这样。但根据您在数据库中的 DATE 格式,您必须更改2012-10-012012-10-03

$this->db->select('*');
$this->db->from('topics_list');
$this->db->where('order_datetime <','2012-10-03');
$this->db->where('order_datetime >','2012-10-01');

$result = $this->db->get();

回答by Alireza

You can specify you $whereand use active records

您可以指定您$where并使用活动记录

$where = "DATE(order_datetime) BETWEEN '2012-10-01' AND '2012-10-3'";
$this->db->where($where)->get('table_name');

回答by user2079277

`$where = array(
  "order_datetime <" => "2012-10-03",
  "order_datetime >" => "2012-10-01"
);
$this->db->select("*")->get_where("topics_list" , $where)`

回答by louk

To use the "Between" by respecting the Query builder of CI:

要通过尊重 CI 的查询构建器来使用“之间”:

$this->db->where("order_datetime BETWEEN '2018-10-01' AND '2018-10-3'","", FALSE);

回答by Usman Zia

You can specify you $where and use active records

您可以指定您 $where 并使用活动记录

$this->db->group_start()
          ->or_where("product_order.generate_date >= ","$start_date")
          ->or_where("product_order.generate_date <","$end_date + INTERVAL 1 DAY")
          ->group_end();