使用括号时出现COALESCE错误

时间:2016-12-07 10:44:55

标签: php mysql laravel

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,这就是我尝试做的事情。

1 个答案:

答案 0 :(得分:0)

COALESCE用法错误。改变它

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