yii2:从createCommand中删除逗号

时间:2016-09-25 07:53:02

标签: mysql yii2-advanced-app

我在我的控制器中创建了一个命令,如下所示:

public function actionTotal($id)
{
     $query1 = new Query;
     $query1  ->select('sum(patient_services.price) price, sum(receipts.price) receipts ,') 
         ->from('patient_services  ')
         ->leftJoin('  receipts  ON 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(receipts.price) receipts FROM `patient_services` LEFT JOIN `  receipts  ON` `patient_services`.`patient_id=receipts`.`patient_id` WHERE patient_services.patient_id=1

当我从sql代码中删除所有逗号并在phpmyadmin中尝试它...它工作正常:(

1 个答案:

答案 0 :(得分:2)

你有一个无效的leftJoin替换它:

->leftJoin('receipts', 'patient_services.patient_id = receipts.patient_id')

你的选择查询结尾处似乎还有一个额外的逗号删除了最后一个逗号,select查询看起来像这样:

$query1  ->select('sum(patient_services.price) price, sum(receipts.price) receipts')

希望这有效。