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,这就是我想要做的。
发布于 2016-12-07 10:52:55
COALESCE的使用是错误的。改变它就像
COALESCE (tele_plan.plan_cost, 0) - COALESCE(SUM(tele_payment_items.amount), 0) as balancehttps://stackoverflow.com/questions/41015354
复制相似问题