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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:35:10+00:00 2026-06-02T12:35:10+00:00

i have two tables i my database in which i have two columns in

  • 0

i have two tables i my database in which i have two columns in each table one is for userid & one is for showing some numeric data.

I just want to add the value of the numeric column but the issue is that it’s not compulsory that both the table have same used id.

I will try to explain it using example:

table1

userid  score1

  1       200
  2       300


table2

userid  score2

  1       400

so now what i want to do is to sum the score form these two tables as per the user id and show it in a new table like this:

userid  score_sum

  1       600
  2       300

if i simply use

Select sum(table1.score1+table2.score2) 
FROM table1 JOIN table2 ON table1.id=table2.id

then it will only show the sum for userid 1 because userid 2 is not present in the second table.So please suggest me how to solve this issue.

  • 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-02T12:35:14+00:00Added an answer on June 2, 2026 at 12:35 pm

    Normally you would solve this problem by doing a full outer join, but MySql does not support such joins so it needs to be faked.

    The query

    SELECT SUM(COALESCE(table1.score1,0) + COALESCE(table2.score2,0))
    FROM (
        SELECT DISTINCT(id) FROM (
            SELECT userid FROM table1 UNION SELECT userid FROM table2
        ) tmp
    ) userids
    LEFT JOIN table2 ON table2.userid = userids.userid
    LEFT JOIN table1 ON table1.userid = userids.userid
    GROUP BY table1.userid
    

    Explanation

    First of all we need to get a list of all the user ids that are present in either table1 or table2 so we can use this list as the dataset to do a LEFT OUTER JOIN from. Any approach that starts the join from table1 or table2 is not going to cut it because if the selected table does not include some user id X then that user will not appear at all in the final results.

    Having created the userids resultset through the subselect we LEFT JOIN both interesting tables to get the scores for each user. If a user is not present in both tables then we ‘re going to have a score of NULL for that user (because NULL + number gives NULL), so we use COALESCE to convert any NULL to 0.

    I should note that this query will work correctly irrespective of which user is included (or not) in which table, or of the order that the tables appear in the query.

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

Sidebar

Related Questions

I have two tables with hierarchyid fields, one of which is a staging table
I have a table and two databases which have the same table, but one
I have two tables in my database: user and media_contact . The media_contact table
I have two tables, each one has a primary ID column as key. I
I currently have two joined tables in a SQL Server database, one with news
I have a database table where each row (movie) has a couple of numeric
I have two models 'Company' and 'Vehicle' And two tables in database named same
I have two database tables, grouptypes and groups. A grouptype can have multiple groups,
I have two database tables with the following structure: actions: action_id int(11) primary key
I have two tables in my database, called ratings and movies . Ratings: |

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.