我已经创建了命令并且它工作正常但是当我添加 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 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 和 ->
$query1->select(["sum(patient_services.price) AS price",
"sum( IFNULL(receipts.price,0)) AS receipts"] )
并最终尝试清除运行时目录..并刷新数据库缓存
关于mysql - yii2 : add IFNULL exception to Command,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39686102/