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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:57:25+00:00 2026-06-15T21:57:25+00:00

I have a MySQL-Database where I run certain Stored Procedures. In one of them,

  • 0

I have a MySQL-Database where I run certain Stored Procedures.

In one of them, I want to sum up the amount a certain user has to pay, the amount he already payed and return all the users, where the amount to be payed isn’t equal the amount paid. I came up with the following (simplyfied) query:

SELECT userId, SUM(costs) as sum_costs, 
  (SELECT SUM(payed)
   FROM payments p WHERE ta.userId=p.userId) as sum_payed
FROM ta 
GROUP BY ta.userId
ORDER BY ta.userId;

This gives me the sum of the costs and the payment for each user. But I do not know, how I can select only the users, where costs and payment are not equal.

WHERE sum_costs != sum_payed 

doesn’t work, because mysql doesn’t know the column ‘sum_costs’ in the WHERE-clause.

Any idea, how to get the selection to work?

  • 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-15T21:57:26+00:00Added an answer on June 15, 2026 at 9:57 pm
    SELECT * FROM 
    (
    SELECT userId, SUM(costs) as sum_costs, 
    (SELECT SUM(payed)
    FROM payments p WHERE ta.userId=p.userId) as sum_payed
    FROM ta 
    GROUP BY ta.userId
    )a
    WHERE a.sum_costs <> a.sum_payed 
    --ORDER BY a.userId;
    

    Update: actually no need for ORDER BY UserId since it will be ordered implicitly by GROUP BY

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

Sidebar

Related Questions

I have shared hosting on CentOS with local MySQL database. I'm able to run
I have mysql database and I want a software which can draw the database
We have a moderately large production MySQL database. Periodically, we will run commands, usually
I have a script that for the most part interacts with one mysql database.
I have a MySQL database running in Amazon RDS , and I want to
I have a MySQL database in which I have to update a certain field
I have set up mysql database to run with java and eclipse on my
I have mySQL database that I see form my Ubuntu shell, I want to
The concept I have uses a central MySql database which has many Java clients
i have use mysql database . my problem is that i run a query

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.