Mysql查询格式问题

时间:2016-03-17 05:43:14

标签: mysql codeigniter

我有一个mysql查询来获取两个日期之间的数据:

select * from `bookings` where `durationFrom`='$new_from_date' and  `durationTo` between '$new_from_date' and '$new_to_date' 

如何按以下方式编写上述查询?

$this->db->select()
                 ->from('bookings')
                 ->where('durationTo',between $new_from_date and $new_to_date)
                 ->where('durationFrom',$new_from_date); 

我正在使用codeigniter ...

这是我的表: enter image description here

我需要在给定的durationFrom和durationTo之间选择数据(durationFrom和durationTo由用户输入)

例如: 如果用户选择durationFrom = 2016-02-25和durationTo = 2016-08-27,我需要获取所有5行

如果用户选择durationFrom = 2016-02-25和durationTo = 2016-05-27,我需要获得前3行

2 个答案:

答案 0 :(得分:0)

你可以这样做

$where = "durationTo between "'.$new_from_date.'" AND "'.$new_to_date.'";
  $this->db->select('*')->from('bookings')->where('durationFrom',$new_from_date)->where($where); 

答案 1 :(得分:0)

试试这个

$this->db->select('*')
    ->from('bookings')
    ->where('durationFrom', $new_from_date)
    ->where("durationTo BETWEEN '".$new_from_date."' AND '".$new_to_date."'")

另一个选项

$this->db->select('*')
    ->from('bookings')
    ->where('durationFrom', $new_from_date)
    ->where('durationTo >= ', $new_from_date)
    ->where('durationTo <= ', $new_to_date)

您可以使用您感兴趣的列名替换select中的*。