2016-09-25 63 views
0

刪除逗號我在我的控制器,這樣創建一個命令:yii2:從createCommand

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 ..它正常工作:(

回答

2

你有一個無效的leftJoin這種替換:

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

也好像你在你的選擇查詢的末尾一個額外的逗號刪除最後一個逗號SELECT查詢應該是這樣的:

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

希望這個作品。

+0

非常感謝你,它工作正常,,,但我有一個新問題,我該如何添加IFNull到我的命令? –

+0

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