2016-07-20 3 views
1

두 개의 테이블 인보이스 및 지불을 사용했습니다. 유료, 미납 및 미 지불 상태를 생성하려고했습니다.mysql 조건 적용

다음은 내 쿼리입니다. 그러면 모든 인보이스와 총 지불액이 지급됩니다. 어떻게 지불 조건을 적용하여 지불되었으므로, 지불되지 않았습니다.

답변

0

이 상태에 문제가 있습니다.

(paidamount >= a.total) 

대신이

(sum(b.amount) >=a.total) 

paidamount처럼 사용하는 것은 투사하면서 이름을 대체 할 단지의 별칭입니다.

+0

총액 (b.amount)> = a.total은 유료 결과를 제공합니다. 감사합니다. –

+0

실제로 @dragoste의 대답도 정확했습니다 .. – Ali786

2

paidamount 결과 단지의 별칭입니다

select a.*, sum(b.amount) as paidamount from tbl_invoices a left join tbl_billpayment b on a.invoiceno = b.invoiceno where a.id != '' GROUP BY b.invoiceno ORDER BY a.billdate DESC LIMIT 0,10 

이 줄 것이다

select a.*, sum(b.amount) as paidamount from tbl_invoices a left join tbl_billpayment b on a.invoiceno = b.invoiceno where a.id != '' and (paidamount >= a.total) 

Notice: Error: Unknown column 'paidamount' in 'where clause'

귀하의 지원에 감사드립니다 오류 : 내가 지불 한 금액에 따라 로 줄 어차피 성공 쿼리입니다 하지만 WHERE 절에는 사용할 수 없습니다.

은 동일한 표현 sum(b.amount) 대신 당신이 WHERE 절에 집계 함수를 사용하고 있기 때문에

select a.*, sum(b.amount) as paidamount 
from tbl_invoices a 
left join tbl_billpayment b on a.invoiceno = b.invoiceno 
where a.id != '' and (sum(b.amount) >= a.total) 

는 또한이 실제로 하나 제대로 작동하지 않을 것 paidamount 별칭을 사용해야합니다. SUM 값은 그룹별로 산출하면서

WHERE는 그룹화 전에일어난다. 여기서 HAVING 절을 사용하고 싶습니다.

select a.*, sum(b.amount) as paidamount 
from tbl_invoices a 
left join tbl_billpayment b on a.invoiceno = b.invoiceno 
where a.id != '' 
GROUP BY a.id 
HAVING sum(b.amount) >= a.total