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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:29:54+00:00 2026-06-07T11:29:54+00:00

SELECT User.username, IFNULL(SUM(credit) – SUM(debit), 0) AS total, ( SELECT SUM(Bid.credit) – SUM(Bid.debit) AS

  • 0
SELECT User.username,
       IFNULL(SUM(credit) - SUM(debit), 0) AS total,
       ( SELECT SUM(Bid.credit) - SUM(Bid.debit) AS freebids
           FROM users AS User
           LEFT
           JOIN bids AS Bid
             ON Bid.user_id = User.id
          WHERE Bid.type = 2
       ) AS FreeBids,
       ( SELECT SUM(Bid.credit) - SUM(Bid.debit) AS Normalbids
           FROM users AS User
           LEFT
           JOIN bids AS Bid
             ON Bid.user_id = User.id
          WHERE Bid.type = 0
             OR Bid.type = 1
       ) AS NormalBids
  FROM users AS User
  LEFT
  JOIN bids AS Bid
    ON Bid.user_id = User.id
 GROUP
    BY User.id

here is my sample table:
for users

id username 
1  user1
2  user2
3  user3
4  user4

For bids

id user_id debit credit type
1  1       0     10     0
2  1       1     5      2 
3  1       1     0      2
4  3       0     10     0
6  2       0     10     0
7  4       1     10     0
8  4       1     0      1

But I am having problem in displaying the subquery (Freebids and Normalbids) are all has the same value here are the sample display:

username    total   FreeBids    NormalBids
user1       10       12809          965
user2       20       12809          965
user3       9        12809          965
user4       0        12809          965

I can’t figure out where did I go worng in my query.
Any suggestion to solve my problem?
Thank you for your help.

  • 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-07T11:29:57+00:00Added an answer on June 7, 2026 at 11:29 am

    The reason why all the same values showed up was because there was no distinction between the users table in the outer query and the users table in the inner query. They both have the same alias. You must distinguish either the outer users table or the inner users table by assigning it with a different alias (such as u) so that the subquery knows which table to reference for the outer value:

    ... FROM users AS u ...
    

    Having said that, it is highly inefficient to use subqueries like that as they would need to be executed for each row in your users table (we’re talking about entire tables needing to be joined and filtered as many times as there are users x2).

    Your query could be rewritten MUCH more efficiently using conditional aggregation:

    SELECT
        a.username,
        IFNULL(SUM(b.credit) - SUM(b.debit), 0) AS total,
        SUM(IF(b.type = 2, b.credit, 0)) - SUM(IF(b.type = 2, b.debit, 0)) AS freebids,
        SUM(IF(b.type IN (0,1), b.credit, 0)) - SUM(IF(b.type IN (0,1), b.debit, 0)) AS normalbids
    FROM users a
    LEFT JOIN bids b ON a.id = b.user_id
    GROUP BY a.id, a.username
    

    Which basically says: SUM the credit/debit ONLY if the type is x value, and you can SUM/COUNT/AVG/etc. on any condition you want just working with one table join.


    SQL-Fiddle Demo

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

Sidebar

Related Questions

Say I have the following: $query = SELECT user_id,email,password, roles.role_id,role_name FROM users JOIN roles
I Want To Join 2 Selections one: SELECT user_email FROM users WHERE user_id =
I have a query like this SELECT user_id FROM user WHERE user_id NOT IN
I have three tables to define users: USER: user_id (int), username (varchar) USER_METADATA_FIELD: user_metadata_field_id
I have following LINQ to SQL query expression from msg in TblUserMessages join user
I have a database table from which, when I select data with username and
hy! I call my webservice from android: HttpConnection con = new HttpConnection(XXX/login, handler); con.setParameter(username,user.getText().toString());
I have a following query: SELECT users.user_id as user_id , users.USERNAME, users.FIRSTNAME, users.LASTNAME, roles.name
Updated question: Django is giving me the following sql query: SELECT auth_user.id, auth_user.username, auth_user.first_name,
Consider this T-SQL: CREATE USER my_test_user WITHOUT LOGIN; SELECT USER_NAME(), USER, SUSER_SNAME(),SYSTEM_USER, suser_name(); EXECUTE

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.