获取日期范围之间的记录

时间:2016-05-25 09:19:16

标签: php codeigniter

我想获取特定日期范围内的行

我的查询:

SELECT * FROM `table` WHERE ((s_tdate <= '2016-05-26' OR `e_expiredate` >= '2016-05-26') OR (`s_tdate` <= '2017-09-11' AND `e_expiredate` >= '2017-09-11') OR (`s_tdate` >= '2016-05-26' AND e_expiredate <='2017-09-11'))

  $this->db->select(*);
  $this->db->where("((s_tdate <='$start_date' AND e_expiredate >= '$start_date')");
  $this->db->or_where("(s_tdate <='$end_date' AND e_expiredate >='$end_date')");
  $this->db->or_where("(s_tdate >='$start_date' AND e_expiredate <='$end_date'))");
  $query = $this->db->get('table');

没有结果。

请帮忙

1 个答案:

答案 0 :(得分:1)

OR 条件置于......

之间
e.g. (s_tdate <= '2016-05-26' AND e_expiredate >= '2016-05-26') OR (s_tdate <= '2017-09-11' AND e_expiredate >= '2017-09-11') OR (s_tdate >= '2016-05-26' AND e_expiredate <='2017-09-11') 

请使用此..

基本上它会在给定范围内提供类似s_tdate和e_expiredate的查询,并提供特定日期范围内的行

$this->db->select(*);
$this->db->where("s_tdate BETWEEN $start_date AND $end_date");
$this->db->where("e_expiredate BETWEEN $start_date AND $end_date");
$query = $this->db->get('table');