I’ve 2 tables:
Teams:
|-- id -- username --|
|__ 1 __ user1 __|
|__ 2 __ user2 __|
|__ 3 __ user3 __|
Payments:
|-- id -- user_id -- amount --|
|__ 1 __ 1 __ 1000 --|
|__ 2 __ 1 __ 5000 --|
|__ 3 __ 2 __ 3000 --|
|__ 4 __ 1 __ 4500 --|
|__ 5 __ 2 __ 1000 --|
I want to get users total payments. in single query. result like this:
|-- user_id -- username -- total_payment --|
|__ 1 __ user1 __ 10500 --|
|__ 2 __ user2 __ 4000 --|
Thanks.
If you only want the
user_idwithoutusername, there is no need for a join at all since the grouping need only happen against theuser_idinPayments.Edit: The question has since been edited to include the username in output, so use the query below — a join is necessary.
If you want to join in the username, it is a simple addition to the
FROMandGROUP BYclauses.