子查询返回多行mysql

时间:2015-09-16 07:43:41

标签: mysql

以下查询必须返回所有客户信息,但不幸的是子查询返回多行错误:

SET @total=0;    
SET @amountDue=0;
SELECT c.customer_name, 
        l.paid_date, 
        IF(@total=0,((SELECT ll.total_amount    
                    FROM payment_loan ll 
                    join payments_details le on ll.loan_id=le.loan_id)-        
                    (SELECT ll.installment_amount_month 
                    FROM payment_loan ll 
                    join payments_details le on ll.loan_id=le.loan_id)*
                    (SELECT ll.total_installments        
                    FROM payment_loan ll 
                    join payments_details le on ll.loan_id=le.loan_id)),
                    (SELECT ll.installment_amount_month 
                    FROM payment_loan ll 
                    join payments_details le on ll.loan_id=le.loan_id)) AS AmountDue,
        @amountDue:=@amountDue+(SELECT IF(@total=0,((SELECT ll.total_amount 
                                                    FROM payment_loan ll 
                                                    join payments_details le on ll.loan_id=le.loan_id)-
                                                    (SELECT ll.installment_amount_month 
                                                    FROM payment_loan ll 
                                                    join payments_details le on ll.loan_id=le.loan_id)*
                                                    (SELECT ll.total_installments 
                                                    FROM payment_loan ll 
                                                    join payments_details le on ll.loan_id=le.loan_id)),
                                                    (SELECT ll.installment_amount_month 
                                                    FROM payment_loan ll 
                                                    join payments_details le on ll.loan_id=le.loan_id)) )as ComulativeDue, 
        l.amount AS AmountPaid, 
        @total := @total +l.amount AS comulativePaid,
        (@total/@amountDue ) as percentage 
FROM payments_details l 
join customer c on c.customer_id= l.customer_id 
where c.customer_id=l.customer_id 
GROUP BY l.paid_date 
ORDER BY l.paid_date 
DESC LIMIT 1 ;

0 个答案:

没有答案