2016-09-25 13 views
1

をコマンドにIFNULL例外を追加し、私は、コマンドを作成してきたし、それが正常に動作しますが、私はIFNULL例外を追加するとき、それはカンマを追加し、私は何の答えは私のコードが存在しない多くのことを検索:yii2:

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); 
} 

Iそれを試してみてください...セレクトコードはコンマを持っているとidon'tはあなたのコードでは、それ

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 

enter image description here

答えて

1

IFNULL関数の2番目のパラメータとして0を追加します。値がnullの場合は0が出力されます。例を示します。

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

私は質問を更新しました:( –

+0

私は自分のansを編集しました。あなたはあなたのselectステートメントに括弧[]を入れる必要があります。 –

3

を削除する方法を知っている

$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));   

その後、あなたのimgに奇妙な結果を見て

はこのこの表記法を使用してみてください

$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)); 

試してみて、(もQuery1をと->

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

との間に2つのスペースを削除最終的にランタイムディレクトリをクリアしようとすると、dbキャッシュをフラッシュします。

+0

私はそれを得ることはありません:( –

+0

私は答えの希望は今や明らかで更新しています... – scaisEdge

+0

私はこの中には、検査のように、それは示してみたときに申し訳ありません...合計(IFNULL(receipts.price、 '0) ) –