How to filter data in the below query using where clause for Balance column. I'm getting the error [Unknown column "Balance" in "where clause"] when i use Balance in where condition.
select *,(it.Total - p.Amount) as Balance
from invoices i
left outer join invoice_items it
on i.ID=it.InvoiceID
left outer join payment p
on p.InvoiceID=it.InvoiceID
where Balance!=0;
And also, when there is no matching payment record found, instead of displaying the null value in Balance column, i need the Total value of the invoice_items table.