如何使用'AND','OR'子句在两个日期之间选择数据?

时间:2017-07-25 06:31:15

标签: php sql codeigniter

我正在尝试从两个日期之间的数据库表中获取数据,并且它还有两个使用“OR”子句的过滤器,但结果是我在日期之间获取数据或使用“OR”子句获取数据。我发布了在这里查询模型,如果有人知道解决方案,请告诉我。 提前致谢

$this->db->select('*');
$this->db->from('Transaction');
$this->db->join('Users', 'Transaction.user_id = Users.id');
$this->db->where('date >=',$startDate);
$this->db->where('date <=',$endDate);
$this->db->where( 'against = Recharge');
$this->db->or_where('against =','Rech_Commission');
$query = $this->db->get();
return $query->result();

我希望日期过滤器是强制性的,但是想要获取记录= = Recharge或者= ='Rech_commission'但是我要么使用日期范围要么反对='Rech_Commission'

3 个答案:

答案 0 :(得分:1)

使用

$where = '(against="Recharge" or against = "Rech_Commission")';
$this->db->where($where);

你有日期和日期以及(反对=''或反对='')

答案 1 :(得分:1)

    $this->db->select('*');
    $this->db->from('Transaction');
    $this->db->join('Users', 'Transaction.user_id = Users.id');
    $this->db->where('date BETWEEN "'. date('Y-m-d', strtotime($startDate)). '" and "'. date('Y-m-d', strtotime($endDate)).'"');
    $this->db->where('against="Recharge" OR against = "Rech_Commission")';
    $query = $this->db->get();
    return $query->result();

答案 2 :(得分:0)

$this->db->select('*');
$this->db->from('Transaction');
$this->db->join('Users', 'Transaction.user_id = Users.id');
$this->db->group_start();
$this->db->where('date >=',$startDate);
$this->db->where('date <=',$endDate);
$this->db->where( 'against = Recharge');
$this->db->group_end();
$this->db->or_where('against =','Rech_Commission');
$query = $this->db->get();
return $query->result();