从星期二开始按周分组使用时在哪里的Codeigniter问题

时间:2018-12-31 09:34:17

标签: php mysql codeigniter

我必须在CodeIgniter查询中使用yearweek()将数据从星期二开始按周进行分组。那么主要的问题是过滤器不适用于星期二日期。仅过滤了yearweek()中星期天的日期。

我通过编写运行良好的原始查询来进行尝试。然后我在查询中进行分析,然后发现了问题。那在列和值之间不显示等号(=)。我将我的CodeIgniter代码和正确的原始查询放在下面,然后将当前的CodeIgniter等效查询放在下面

function getbranchSalesreportshift($limit,$offset,$order_column,$order_type,$parameters)
{
     $this->db->select("str_to_date(concat(yearweek(s.creationdate), 'tuesday'), '%X%V %W') as week,b.name as branchname,g.name as groupname,st.name as shiftname,count(*) as total_sales_party,sum(s.price) as total_sales,count(case when salesAdvanceAmount > 0 then s.customerid end) as total_advance_party,sum(case when salesAdvanceAmount > 0 then salesAdvanceAmount end) as total_advance_amount,count(CASE WHEN DATE(s.creationdate) <> Date(customerCreationDate) then 1 end ) as old_sales_party,sum(CASE WHEN DATE(s.creationdate) <> Date(customerCreationDate) THEN s.price END ) as old_sales_amount,count(CASE WHEN DATE(s.creationdate) = Date(customerCreationDate) THEN 1 END ) as new_sales_party,sum(CASE WHEN DATE(s.creationdate) = Date(customerCreationDate) THEN s.price END ) as new_sales_amount,count(returnID) as return_product,sum(orn.price) as return_amount ");
     if($parameters['shift']!=NULL){
       $this->db->like('s.shiftID',$parameters['shift']);
    }
    if($parameters['cdatefrom']!=NULL){
       // $this->db->where('yearweek(s.creationdate)',date('YW',strtotime($parameters['cdatefrom'])));
      $this->db->where('str_to_date(concat(yearweek(s.creationdate), "tuesday"), "%X%V %W")',date('YW',strtotime($parameters['cdatefrom'])));
    }
    $this->db->group_by(array("s.shiftID", "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);
    }
     $uid=$this->session->userdata('id'); 
    $this->db->where('b.ownerid',$uid); 
    $this->db->join('open_returns orn','s.branchID=orn.branchID ','left');
    $this->db->join('shifts st','s.shiftID=st.id ','left');
    $this->db->join('group g','s.groupID=g.id ','left');
    $this->db->join('branch b','s.branchID=b.id ','left');
    $query = $this->db->get('super_sales s');
    if($query->num_rows()>0){
        return $query->result_array();
    }else{
        return FALSE;
    }
}

这是我等价的原始查询(列和值之间的位置附近不能放置等号)

SELECT str_to_date(concat(yearweek(s.creationdate), 'tuesday'), '%X%V %W') as week, `b`.`name` as `branchname`, `g`.`name` as `groupname`, `st`.`name` as `shiftname`, count(*) as total_sales_party, sum(s.price) as total_sales, count(case when salesAdvanceAmount > 0 then s.customerid end) as total_advance_party, sum(case when salesAdvanceAmount > 0 then salesAdvanceAmount end) as total_advance_amount, count(CASE WHEN DATE(s.creationdate) <> Date(customerCreationDate) then 1 end ) as old_sales_party, sum(CASE WHEN DATE(s.creationdate) <> Date(customerCreationDate) THEN s.price END ) as old_sales_amount, count(CASE WHEN DATE(s.creationdate) = Date(customerCreationDate) THEN 1 END ) as new_sales_party, sum(CASE WHEN DATE(s.creationdate) = Date(customerCreationDate) THEN s.price END ) as new_sales_amount, count(returnID) as return_product, sum(orn.price) as return_amount FROM `super_sales` `s` LEFT JOIN `open_returns` `orn` ON `s`.`branchID`=`orn`.`branchID` LEFT JOIN `shifts` `st` ON `s`.`shiftID`=`st`.`id` LEFT JOIN `group` `g` ON `s`.`groupID`=`g`.`id` LEFT JOIN `branch` `b` ON `s`.`branchID`=`b`.`id` WHERE str_to_time(concat(yearweek(s.creationdate), "tuesday"), "%X%V %W") '201842' AND `b`.`ownerid` = '189' GROUP BY `s`.`shiftID`, str_to_date(concat(yearweek(s.creationdate), 'tuesday'), '%X%V %W') ORDER BY `week` DESC LIMIT 50 

我被发现如下正确的原始查询,但是我需要正确的CodeIgniter编码用于相应的过滤器/位置

SELECT str_to_date(concat(yearweek(creationdate), 'sunday'), '%X%V %W') as week,b.name as branchname,g.name as groupname,st.name,count(*) as total_sales_party,sum(price) as total_sales,count(case when salesAdvanceAmount > 0 then customerid end) as total_advance_party,sum(case when salesAdvanceAmount > 0 then salesAdvanceAmount end) as total_advance_amount,count(CASE WHEN DATE(creationdate) <> Date(customerCreationDate) then 1 end ) as old_sales_party,sum(CASE WHEN DATE(creationdate) <> Date(customerCreationDate) THEN price END ) as old_sales_amount,count(CASE WHEN DATE(creationdate) = Date(customerCreationDate) THEN 1 END ) as new_sales_party,sum(CASE WHEN DATE(creationdate) = Date(customerCreationDate) THEN price END ) as new_sales_amount FROM `super_sales` `s` LEFT JOIN branch b ON `s`.branchID=b.id LEFT JOIN `group` g ON `s`.groupID=g.id LEFT JOIN shifts st ON s.shiftID=st.id where str_to_date(concat(yearweek(s.creationdate), 'tuesday'), '%X%V %W') = '2018-10-16' AND b.ownerid=189 GROUP BY str_to_date(concat(yearweek(s.creationdate), 'tuesday'), '%X%V %W'),s.shiftID ORDER BY week DESC 

我需要按select中的星期列之类的星期二工作日过滤数据

1 个答案:

答案 0 :(得分:0)

这是一个非常艰难的过程-因为,您必须了解查询生成器和底层数据库驱动程序的工作方式。

问题在于,通常情况下,Querybuilder后缀为where条件,给定键的默认值为=。但是,在您的情况下,并不是因为它认为它具有运算符。

这恰好发生在以下几行中:

https://github.com/bcit-ci/CodeIgniter/blob/develop/system/database/DB_query_builder.php#L681

https://github.com/bcit-ci/CodeIgniter/blob/develop/system/database/DB_driver.php#L1485

  

您可能在这里发现了一个错误-最好的办法是在Forum上进行报告。

您目前的解决方案是自己添加后缀:

$this->db->where('str_to_date(concat(yearweek(s.creationdate), "tuesday"), "%X%V %W") =',date('YW',strtotime($parameters['cdatefrom'])));