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 8280061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:32:08+00:00 2026-06-08T09:32:08+00:00

I have an application with a users table, credit_purchase table, and credit_spend table. I

  • 0

I have an application with a users table, credit_purchase table, and credit_spend table. I am trying to get the amount of credits the user currently has.

The credit tables are as follows:

credit_purchase

+----+---------+-------+---------------------+
| id | user_id | coins | timestamp           |
+----+---------+-------+---------------------+

credit_spend

+----+---------+-------+---------------------+
| id | user_id | coins | timestamp           |
+----+---------+-------+---------------------+

What I’m trying to do is select the SUM(credit_purchase.coins) – SUM(credit_spend.coins) for each user. Right now I have something like this:

SELECT users.*, COALESCE(SUM(credit_purchase.coins) - SUM(credit_spend.coins), 0) 
FROM users 
LEFT JOIN credit_purchase ON users.id = credit_purchase.user_id 
LEFT JOIN credit_spend ON users.id = credit_spend.user_id 
GROUP BY users.id

but, it the result is not the correct number. It seems to be SUMing the purchases properly, but multiplying the SUM from the spend by the number of purchases.

What am I doing wrong?

Thanks!

  • 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-08T09:32:10+00:00Added an answer on June 8, 2026 at 9:32 am

    try this:

    SELECT  users.*, 
            COALESCE(SUM(credit_purchase.coins) - SUM(credit_spend.coins), 0) 
    FROM    users 
                LEFT JOIN credit_purchase
                        ON users.id = credit_purchase.user_id 
                LEFT JOIN credit_spend 
                        ON users.id = credit_spend.user_id 
    GROUP BY users.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application which has a Projects table and a Users table. In
I have two tables, a user table and a application table: User id username
In my JPA2/Hibernate application I have table USERS and matching entity User + DAO.
I'm currently integrating Facebook Connect with my application which has an existing users table.
I basically have two tables called images and users . The images table has
In my application I have the following tables: Posts id title content Users id
My application database has a Groups table that separates users into logical roles and
Scenario : I have a users table in my application. I also have two
I have a pretty typical user table setup for my web application: user_id |
[edit] We're collecting credit application data from users on a web form. I have

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.