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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:37:43+00:00 2026-06-02T14:37:43+00:00

I’ve 2 tables: Teams: |– id — username –| |__ 1 __ user1 __|

  • 0

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.

  • 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-02T14:37:44+00:00Added an answer on June 2, 2026 at 2:37 pm

    If you only want the user_id without username, there is no need for a join at all since the grouping need only happen against the user_id in Payments.

    SELECT 
      user_id, 
      SUM(amount) AS total
    FROM Payments
    GROUP BY user_id
    

    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 FROM and GROUP BY clauses.

    SELECT 
      Teams.user_id, 
      username,
      SUM(amount) AS total
    FROM 
      Teams
      /* LEFT JOIN used in case a user has no payments -- will still show in the list */ 
      LEFT JOIN Payments ON Teams.user_id = Payments.user_id
    GROUP BY 
      Teams.user_id, 
      Teams.username
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
string formIdList = 8256, 8258, 8362, 8120, 8270, 8271, 8272, 8273, 8257, 8279, 8212,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

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.