今天,当我尝试使用带有yearweek()
的日期字段进行过滤时,我在CodeIgniter框架中遇到了问题。这是我没有CodeIgniter样式定义的普通查询
SELECT b.name as branch,str_to_date(concat(yearweek(gp.createddate), 'sunday'), '%X%V %W') as week,sum(case when amount>0 then qty else 0 end) as purchase_quantity,sum(case when amount>0 then gp.amount else 0 end) as purchase_amount,sum(case when amount<0 then qty else 0 end) as return_quantity,sum(case when amount<0 then gp.amount else 0 end) as return_amount FROM `group_purchase` gp LEFT JOIN `branch` b ON gp.branchid=b.id WHERE yearweek(gp.createddate)=yearweek('2018-12-24') GROUP BY gp.branchid,yearweek(gp.createddate) ORDER BY yearweek(gp.createddate) DESC
那很好,结果得到了正确的显示。我使用yearweek()
来按周对数据进行分组。当我将这个查询实现到codeigniter中时,除yearweek()
之外的所有其他东西都运行良好。在下面的查询中附加了我的codeigniter模型
function getPurchasereport($limit,$offset,$order_column,$order_type,$parameters)
{
$this->db->select("b.name as branchname,str_to_date(concat(yearweek(gp.createddate), 'sunday'), '%X%V %W') as week,sum(case when amount>0 then qty else 0 end) as purchase_quantity,sum(case when amount>0 then gp.amount else 0 end) as purchase_amount,sum(case when amount<0 then qty else 0 end) as return_quantity,sum(case when amount<0 then gp.amount else 0 end) as return_amount");
if($parameters['branch']!=NULL){
$this->db->like('gp.branchid',$parameters['branch']);
}
if($parameters['cdatefrom']!=NULL){
$this->db->where('yearweek(gp.createddate)',yearweek("$parameters['cdatefrom'])");
}
$this->db->group_by(array("gp.branchid", "yearweek(gp.createddate)"));
if(empty($order_column) || empty($order_type)){
$this->db->order_by('yearweek(gp.createddate)','asc');
}else{
$this->db->order_by($order_column,$order_type);
}
if($limit!=0){
$this->db->limit($limit,$offset);
}
$this->db->join('branch b','gp.branchid=b.id ','left');
$query = $this->db->get('group_purchase gp');
if($query->num_rows()>0){
return $query->result_array();
}else{
return FALSE;
}
}
它产生如下错误 error
答案 0 :(得分:0)
您的错误是由于此行
if($parameters['cdatefrom']!=NULL){
$this->db->where('yearweek(gp.createddate)',yearweek("$parameters['cdatefrom'])");
}
函数yearweek
不是有效的PHP函数,因此您不能以这种方式使用它。
您需要将其替换为以下内容:
if($parameters['cdatefrom']!=NULL){
$this->db->where('yearweek(gp.createddate)',date('YW',strtotime($parameters['cdatefrom'])));
}