Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9098311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:19:55+00:00 2026-06-17T00:19:55+00:00

This is going to be tough to explain. I’m looping through my client records

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T00:19:57+00:00Added an answer on June 17, 2026 at 12:19 am

    Just join customers to bills and do the sums. To separate out totalpaid and totalowed you can use SUM(CASE or SUM(IF as wless1’s answer demonstrates

    SELECT c.*,
            SUM(CASE WHEN billtype = 1 THEN total ELSE 0 END) totalpaid ,
            SUM(CASE WHEN billtype = 2 THEN total ELSE 0 END) totalowed ,
            SUM(total) AS totalbalance
    FROM 
        tbl_customers c
        LEFT JOIN tbl_customers_bills  b
        ON c.customerid = b.customerid
         and billtype in (1,2)
    GROUP BY 
         c.customerid
    

    Because this is MySQL you only need to group on the PK of customer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This one is tough to explain inside the title. What I have going on
This is going to be a bit difficult to explain. I'm usually fine with
So I'm going to explain this with an example. I have a like button
Alright so this is going to be a doozy to explain. I'm making a
I am going through this tutorial on how to perform some work in the
I am going to do my best to explain this, though admittedly I have
I'm going to try and explain this the best I can. I used this
This is going to take a bit to explain. I'm creating my first real-world
Can you please explain what's going on in this buggy example: Base base; Derived*
Dear all, this is going to be tough: I have created a game object

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.