2016-12-07 34 views
0
select 
tele_plan.id as value, 
tele_plan.plan_name as label, 
tele_plan.plan_cost as c, 
tele_plan.plan_details as d, 
COALESCE (tele_plan.plan_cost - sum(tele_payment_items.amount) as balance ,0) 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` = 9 
    group by `tele_plan`.`id` 

我在這裏使用laravel框架。我想這裏的問題是COALESCE。我試圖通過減去兩列值來找到餘額。如果沒有任何回報作爲balance它應該設置爲0,這是我正在嘗試做的。COALESCE使用帶括號的錯誤

+0

歡迎來到堆棧溢出!您可以先參加[tour](http://stackoverflow.com/tour)並學習[如何提出一個好問題](http://stackoverflow.com/help/how-to-ask)並給我們* *錯誤信息**請。我們會更容易幫助你。 –

回答

0

COALESCE用法是錯誤的。改變它像

COALESCE (tele_plan.plan_cost, 0) - COALESCE(SUM(tele_payment_items.amount), 0) as balance 
+0

是的,它的工作。謝謝。 –