2016-12-08 44 views
2

我命名爲PlanCostDiscountAmountPaid列,其中包含Due在whereraw /凡在laravel框架

這裏標籤中使用SUM,我想在PlanCost != AmountPaid

這是隻顯示行我的整個查詢

$getID = isset($inputArray['id']) ? $inputArray['id'] : 0; 
    $query = TelePlanSelect::join('tele_plan', 'tele_plan.id', '=', 'tele_plan_select.teleplan_id' ) 
     ->leftjoin('tele_payment_defs', 'tele_payment_defs.telereg_id', '=', 'tele_plan_select.telereg_id') 
     ->leftjoin('tele_payment_items', function ($join){ 
      $join->on('tele_payment_items.telepaymentdefs_id', '=', 'tele_payment_defs.id') 
       ->on('tele_payment_items.teleplan_id', '=' ,'tele_plan.id'); 
     }) 

     ->selectRaw('tele_plan.id as `PlanID`,' . 
      'tele_plan.plan_name as `PlanName`,' . 
      'tele_plan.plan_cost as `PlanCost`,' . 
      'tele_plan.plan_details as `PlanDetails`,'. 
      'sum(tele_payment_items.amount) as `AmountPaid` ,'. 
      'COALESCE(sum(tele_payment_items.discount),0) as `Discount`,'. 
      '(COALESCE(tele_plan.plan_cost,0) - sum(COALESCE(tele_payment_items.discount,0))) - sum(coalesce(tele_payment_items.amount, 0)) as `Due` ' 

     ) 
     ->where('tele_plan_select.telereg_id', $getID) 
     ->groupBy('tele_plan.id') 
     ->where(' tele_plan.plan_cost', '!=', 'sum(tele_payment_items.amount)'); 

我不知道我們是否能在where/whereraw 可能有人h使用sum elp我?

編輯1: {"message":"SQLSTATE[HY000]: General error: 1111 Invalid use of group function (SQL: select count(*) as aggregate from (select tele_plan.id as PlanID ,tele_plan.plan_name as PlanName ,tele_plan.plan_cost as PlanCost ,tele_plan.plan_details as PlanDetails ,sum(tele_payment_items.amount) as AmountPaid ,COALESCE(sum(tele_payment_items.discount),0) as折扣,(COALESCE(tele_plan.plan_cost,0) - sum(COALESCE(tele_payment_items.discount,0))) - sum(coalesce(tele_payment_items.amount, 0)) as由於from tele_plan_select inner join tele_plan on tele_plan . ID = tele_plan_select . teleplan_id left join tele_payment_defs on tele_payment_defs . telereg_id = tele_plan_select . telereg_id left join tele_payment_items on tele_payment_items . telepaymentdefs_id = tele_payment_defs . ID and tele_payment_items . teleplan_id = tele_plan . ID where tele_plan_select . telereg_id = 8 and tele_plan.plan_cost <> sum(tele_payment_items.amount) group by tele_plan . ID ) as a) @ C:\\xampp\\htdocs\\halframework\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php:625","status":"failed"}

回答

0

您可以使用原始的SQL功能,如sum,但你必須要包起來laravel的\DB::raw()功能,使他們贏得了」不要逃跑。

爲了您Where條款,你應該做到以下幾點

->where('tele_plan.plan_cost', '<>', \DB::raw('sum(tele_payment_items.amount)')); 

你也可以使用laravel的WhereRaw功能編寫自己的條件:

->whereRaw('tele_plan.plan_cost <> sum(tele_payment_items.amount)') 

更新: 錯誤指出, GroupBy存在問題。原因是因爲一筆款項只能在一組之後才能完成,而一筆不能。所以,這將不得不添加在HAVING clause

由具有更換哪裏,這應該工作:

->havingRaw('tele_plan.plan_cost <> sum(tele_payment_items.amount)'); 
+0

這是不行的,我必須知道的是,我們可以使用'sum'在laravel where子句裏面? –

+0

是的,你可以。你有沒有在我的答案中嘗試過兩個例子?如果是這樣,你可以更新你的問題,並添加你得到的輸出和你期望的輸出嗎? – Jerodev

+0

我添加了錯誤信息,並且在我的問題中已經寫入。我需要行'plan_cost!= tele_payment_items.amout' –