我命名爲PlanCost
Discount
AmountPaid
列,其中包含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"}
這是不行的,我必須知道的是,我們可以使用'sum'在laravel where子句裏面? –
是的,你可以。你有沒有在我的答案中嘗試過兩個例子?如果是這樣,你可以更新你的問題,並添加你得到的輸出和你期望的輸出嗎? – Jerodev
我添加了錯誤信息,並且在我的問題中已經寫入。我需要行'plan_cost!= tele_payment_items.amout' –