【问题标题】:or_where is not working in codeigniter modelor_where 在 codeigniter 模型中不起作用
【发布时间】:2016-05-01 12:40:46
【问题描述】:

我有一个查询模型。但是第二个 or_where 不起作用

型号

    $this->db->select('st_student.st_id');
    $this->db->from('st_student'); 
    $this->db->where('st_status',1);
    $this->db->or_where('st_status',2);  
    if(($from!='') && ($to!='')){  
        $this->db->where("ab_date BETWEEN '$from' AND '$to'");
        $this->db->or_where("as_date BETWEEN '$from' AND '$to'");
    } 
    $this->db->group_by('st_student.st_id');    
    $result=$this->db->get();

sql查询

SELECT `st_student`.`st_id`
FROM (`st_student`)
WHERE `st_status` =  1
OR `st_status` =  2
AND `ab_date` BETWEEN '01/15/2016' AND '01/26/2016'
AND `as_date` BETWEEN '01/15/2016' AND '01/26/2016'
GROUP BY `st_student`.`st_id`

这有什么问题

【问题讨论】:

  • 实际编辑 OP 原始帖子的问题是我们不知道错误是否来自那个小的语法错误 - 也许 OP 想发表评论?
  • @Rose 你是遇到语法错误还是没有得到预期的结果?
  • 我没有得到预期的结果..在mysql中运行我的查询。我在where子句之间加上括号..那个时间得到了正确的结果
  • @Rose-您最初发布的代码在第一行缺少撇号-此代码是直接从您的脚本中复制并粘贴的吗?
  • @Rose $from 和 $to 中的内容

标签: php mysql codeigniter date between


【解决方案1】:

您没有得到预期的结果,因为查询没有条件括号。

尝试如下:

$status_condition = 'st_status = 1 or st_status = 2';
$this->db->select('st_student.st_id');
$this->db->from('st_student');
$this->db->where($status_condition);
if(($from!='') && ($to!=''))
{
    $date_condition = "((ab_date BETWEEN '".$from."' AND '".$to."') or (as_date BETWEEN '".$from."' AND '".$to."'))";
    $this->db->where($date_condition);
}
$this->db->group_by('st_student.st_id');
$result=$this->db->get();

【讨论】:

    【解决方案2】:

    使用or_where_in

    $this->db->select('st_student.st_id');
    $this->db->from('st_student'); 
    $this->db->where('st_status',1);
    $this->db->or_where('st_status',2);  
    if((!empty($from)) && (!empty($to)))
    {  
        $this->db->where('ab_date BETWEEN date("$from", "Y/m/d H:i:s") AND date("$to", "Y/m/d H:i:s") ');
        $this->db->or_where_in('as_date BETWEEN date("$from", "Y/m/d H:i:s") AND date("$to", "Y/m/d H:i:s") ');
    } 
    $this->db->group_by('st_student.st_id');    
    $query = $this->db->get();
    $result = $query->result_array();   
    

    【讨论】:

      【解决方案3】:

      试试这个:

      $this->db->select('st_student.st_id');
      $this->db->from('st_student');
      $this->db->where('st_status',1, FALSE);
      $this->db->or_where('st_status',2, NULL, FALSE);
      if(($from!='') && ($to!='')){
          $this->db->where("ab_date BETWEEN '$from' AND '$to'", FALSE);
          $this->db->or_where("as_date BETWEEN '$from' AND '$to'", NULL, FALSE);
      }
      $this->db->group_by('st_student.st_id');
      $result=$this->db->get();
      

      【讨论】:

        【解决方案4】:

        使用

        $this->db->where("st_status = 1 or st_status = 2")
        

        而不是

        $this->db->where('st_status',1);
        
        $this->db->or_where('st_status',2); 
        

        【讨论】:

        • 这不是答案
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-08
        • 1970-01-01
        • 2023-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多