我为mysql的视图编写了一个sql case条件查询,但是我收到了错误:
#1064 - You have an error in your SQL syntax;error at AS `total_amount_paid`, at line 3
以下是我的代码:
create or replace ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `vw_customer_payment_status` AS select `p`.`customer_id` AS `customer_id`,
concat(`l`.`first_name`,'',`l`.`last_name`) AS `customer_name`,
sum(CASE WHEN l.lead_id = p.customer_id THEN`p`.`payment_amount`) AS `total_amount_paid`,
sum(CASE WHEN l.lead_id = c.customer_id THEN `c`.`pending`) AS `total_amount_due`,
(sum(`p`.`payment_amount`) - sum(`c`.`pending`)) AS `difference`,
if(((sum(`p`.`payment_amount`) - sum(`c`.`pending`)) > 0),0,1) AS `status`
from ((`tbl_leads` `l` join `tbl_customer_payments` `p`)
join `tbl_customer_payment_schedule` `c`
on((`l`.`lead_id` = `p`.`customer_id`)))
答案 0 :(得分:0)
您需要将END CASE添加到CASE语句中。就在右括号之前。
sum(CASE WHEN l.lead_id = p.customer_id THEN`p`.`payment_amount` END CASE) AS `total_amount_paid`,
sum(CASE WHEN l.lead_id = c.customer_id THEN `c`.`pending` END CASE) AS `total_amount_due`,