This is going to be tough to explain.
I'm looping through my client records from tbl_customers
several times a day.
SELECT c.* FROM tbl_customers c
I'm returning simply the customer's: customerid
, name
, phone
, email
Now the weird part. I want to append 3 more columns, after email: totalpaid, totalowed, totalbalance BUT, Those column names don't exist anywhere.
Here is how I query each one: (as a single query)
SELECT SUM(total) AS totalpaid
FROM tbl_customers_bills
WHERE customerid = X
AND billtype = 1
SELECT SUM(total) AS totalowed
FROM tbl_customers_bills
WHERE customerid = X
AND billtype = 2
SELECT SUM(total) AS totalbalance
FROM tbl_customers_bills
WHERE customerid = X
AND billtype IN(1,2)
So, the billtype
is the column that tells me whether the record is paid or not.
I am at a loss here. How can I SUM 3 separate queries into the first query's loop?