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

眉间皱痕 提交于 2019-11-27 22:17:02

问题


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.

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

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

回答1:


this is the way . but according to the DATE format you have in the database you have to change 2012-10-01 and 2012-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();



回答2:


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



回答3:


You can specify you $where and use active records

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



回答4:


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

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



回答5:


You can specify you $where and use active records

$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();


来源:https://stackoverflow.com/questions/12102195/how-to-query-sql-with-active-record-for-dates-between-specified-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!