yii2:向命令添加 IFNULL 异常

yii2 : add IFNULL exception to Command

我已经创建了命令并且它工作正常但是当我添加 IFNull 异常时它添加了一个逗号并且我搜索了很多没有答案我的代码:

public function actionTotal($id)
{
     $query1 = new Query;
     $query1  ->select(' sum(patient_services.price) price ,
                sum( IFNULL(receipts.price,0)) receipts') 
              ->from('patient_services')
              ->leftJoin('receipts', 'patient_services.patient_id = receipts.patient_id')
              ->where('patient_services.patient_id=:id', array(':id'=>$id));      
     $command1 = $query1->createCommand();
     $price = $command1->queryAll();  
     echo Json::encode($price);
}

当我尝试时...select 代码有一个逗号,我不知道如何删除它

 SELECT sum(patient_services.price) price, sum( IFNULL(receipts.price, `0))` AS `receipts` FROM `patient_services` LEFT JOIN `receipts` ON patient_services.patient_id = receipts.patient_id WHERE patient_services.patient_id=2

在你的代码中

   $query1  ->select('sum(patient_services.price) price
                 ,sum( IFNULL(receipts.price,)) receipts')
                                            ^^ here is missing the value for ifnull 
                                               eg: ifnull(your_column, 0);
     ->from('patient_services')
     ->leftJoin('receipts', 'patient_services.patient_id = receipts.patient_id')
     ->where('patient_services.patient_id=:id', array(':id'=>$id));        

然后尝试

 $query1  ->select(' sum(patient_services.price) price ,
                    sum( IFNULL(receipts.price,0)) receipts') 
     ->from('patient_services')
     ->leftJoin('receipts', 'patient_services.patient_id = receipts.patient_id')
     ->where('patient_services.patient_id=:id', array(':id'=>$id)); 

查看您的 img 中的奇怪结果

尝试使用这个符号和(同时删除 query1 和 ->

之间的两个 space
$query1->select(["sum(patient_services.price) AS price", 
     "sum( IFNULL(receipts.price,0)) AS receipts"]  )

并最终尝试清除运行时目录..并刷新数据库缓存

添加 0 作为 IFNULL 函数的第二个参数,这样如果值为 null 它将打印 0,示例如下。

$query1->select(['sum(patient_services.price) price,
                sum( IFNULL(receipts.price,0)) receipts'])