2014-11-17 143 views
0

我寫了一個SQL的情況下狀態查詢視圖的MySQL,但我得到了錯誤:錯誤#1064在MySQL語法

#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

您需要添加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`, 
+0

現在我檢查,但它沒有奏效。 –

+0

你是什麼意思,它沒有工作?您可能需要在END CASE之前添加ELSE語句。像'ELSE 0 END CASE'一樣,總和函數總是有總和。 – Robbert