我必须在codeigniter查询中使用yearweek()从星期二开始按周对数据进行分组。这工作正常,没有问题。然后主要的问题是过滤was不适用于星期二日期。在where on sunday dates中只过滤yearweek()。
我试着写了一个运行良好的原始查询。然后我分析了我的问题,然后我发现了问题。在where列和值之间没有显示相等(=)符号。我把我的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;
}
}
这是我的等价原始查询(不能将相等符号放在where附近的列和值之间)
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
我需要过滤的数据,如星期二在选择工作日周列
1条答案
按热度按时间qco9c6ql1#
这是一个相当困难的问题,因为您必须知道查询生成器和底层db驱动程序是如何工作的。
问题是,通常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
你可能在这里发现了一个bug——最好的办法就是在论坛上报告它。
目前,您的解决方案是使用自己的后缀: