Skip to content
Advertisement

Why is MySQL SUM query only returning one row?

I am trying to create a MySQL query to return the sum of all statement balances for each account in my DB. The query looks like this:

When I do this, it returns only one row with one summed balance and one account (account with ID 1). What I want is to return all accounts with their summed statement balance. How would I achieve this?

Advertisement

Answer

You need to group by something, probably handle (I presume that is related to the account id?), otherwise MySQL will SUM all the values selected from your JOIN. Adding a GROUP BY clause makes the SUM happen for each distinct value of the column in the GROUP BY. Change your query to:

If handle is not related to accounts.id and you want to get the results grouped by accounts.id, change the query to:

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement