在codeigniter where类中使用Mysql Interval

时间:2019-01-18 09:57:48

标签: php mysql codeigniter

我在查询中使用了MySQL间隔。在原始类型查询中,它工作正常,但在PHP风格中,还返回了一些错误的数据。我发现由于间隔时间而引起的问题。我尝试了很多,但解决不了。

数字1是我的原始查询(工作正常), 2是我的CodeIgniter代码, 数字3是我对CodeIgniter代码的实际查询,

NO:1 SELECT str_to_date(concat(yearweek(s.creationdate), 'tuesday'), '%X%V %W') as week, `b`.`name` as `branchname`, `g`.`name` as `groupname`, `s`.`streetName`, DATE(s.creationdate) as start_date, DATE(s.maturity_date) as maturity_date, (DATE(s.maturity_date) + INTERVAL 4 WEEK) as nippu_date, `customerName`, `s`.`balance` FROM `super_sales` `s` LEFT JOIN `branch` `b` ON `s`.`branchID`=`b`.`id` LEFT JOIN `group` `g` ON `s`.`groupID`=`g`.`id` LEFT JOIN `super_customer` `c` ON `c`.`customerid`=`s`.`customerid` WHERE DATE(c.lastAttendance) > (DATE(s.maturity_date) + INTERVAL 4 WEEK) AND `c`.`balance` <>0 ORDER BY `week` DESC LIMIT 50

NO 2: $this->db->select("str_to_date(concat(yearweek(s.creationdate), 'tuesday'), '%X%V %W') as week,b.name as branchname,g.name as groupname,s.streetName,DATE(s.creationdate) as start_date,DATE(s.maturity_date) as maturity_date,(DATE(s.maturity_date) + INTERVAL 4 WEEK) as nippu_date,customerName,s.balance");
     if($parameters['branch']!=NULL){
       $this->db->like('s.branchID',$parameters['branch']);
    }
    // $dat = array('e_date' =>  ;
    // $dat=DATE_ADD(DATE(s.maturity_date) + INTERVAL 4 DAY);
    $this->db->where('DATE(c.lastAttendance) >','(DATE(s.maturity_date) + INTERVAL 4 WEEK)');
     $this->db->where('c.balance <>',0);
    if($parameters['cdatefrom']!=NULL){
       $this->db->where('yearweek(s.creationdate)',date('YW',strtotime($parameters['cdatefrom'])));
    }
    // $this->db->group_by(array("s.branchID", "str_to_date(concat(yearweek(s.creationdate), 'tuesday'), '%X%V %W')"));
    if(empty($order_column) || empty($order_type)){
        $this->db->order_by('yearweek(s.creationdate)','asc');
    }else{
        $this->db->order_by($order_column,$order_type);
    }
    if($limit!=0){
        $this->db->limit($limit,$offset);
    }
    $this->db->join('branch b','s.branchID=b.id ','left');
    $this->db->join('group g','s.groupID=g.id ','left');
    $this->db->join('super_customer c','c.customerid=s.customerid ','left');
    $query = $this->db->get('super_sales s');
    echo $this->db->last_query();
    if($query->num_rows()>0){
        return $query->result_array();
    }else{
        return FALSE;
    }

    NO:3 SELECT str_to_date(concat(yearweek(s.creationdate), 'tuesday'), '%X%V %W') as week, `b`.`name` as `branchname`, `g`.`name` as `groupname`, `s`.`streetName`, DATE(s.creationdate) as start_date, DATE(s.maturity_date) as maturity_date, (DATE(s.maturity_date) + INTERVAL 4 WEEK) as nippu_date, `customerName`, `s`.`balance` FROM `super_sales` `s` LEFT JOIN `branch` `b` ON `s`.`branchID`=`b`.`id` LEFT JOIN `group` `g` ON `s`.`groupID`=`g`.`id` LEFT JOIN `super_customer` `c` ON `c`.`customerid`=`s`.`customerid` WHERE DATE(c.lastAttendance) > '(DATE(s.maturity_date) + INTERVAL 4 WEEK)' AND `c`.`balance` <>0 ORDER BY `week` DESC LIMIT 50

如果我删除了(DATE(s.maturity_date)+ INTERVAL 4 WEEK)涵盖的较高单引号,可以比较2个查询,但它工作正常,但是我不知道如何?

0 个答案:

没有答案