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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:41:42+00:00 2026-06-17T08:41:42+00:00

Given are two tables with with a common id and integer values: table1: table2:

  • 0

Given are two tables with with a common id and integer values:

table1:            table2:
| id   | val  |    | id   | val  |
+------+------+    +------+------+
| 1    |   0  |    | 1    |   1  |
| 2    |   1  |    | 1    |   1  |
| 3    |   1  |    |  2   |   2  |
| 4    |   2  |    |  2   |   2  |
| 5    |   2  |    |   3  |   2  |
                   |   3  |   2  |
                   |   3  |   2  |
                   |    4 |   6  |  <id shifted for readability>

How to archive this result in one query:

result-table:
| id   | val  |
+------+------+
| 1    |   2  |
| 2    |   5  |
| 3    |   7  |
| 4    |   8  |
| 5    |   2  |

EDIT:

Following some answers I got:

SELECT t1.id, t1.val + SUM(t2.val) AS val
FROM table1 t1
LEFT JOIN table2 t2 ON (t1.id = t2.id)
GROUP BY t1.id

what gives me:

id  val
1   2
2   5
3   7
4   8
5   NULL

But 2 plus nothing = 2 and not NULL. Is there a different way?

  • 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-17T08:41:43+00:00Added an answer on June 17, 2026 at 8:41 am

    You forgot to group by:

    SELECT t1.id, t1.val - SUM(t2.val) AS val
    FROM t1
    JOIN table2 t2 ON (t1.id = t2.id)
    GROUP BY t1.id
    

    of course, I’m not sure why you’re trying to subtract the values in your sample query. maybe you meant + instead?

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

Sidebar

Related Questions

given the two tables and values: Tables People- Columns: [ID]PK Name Field - Columns:
Given these two queries: Select t1.id, t2.companyName from table1 t1 INNER JOIN table2 t2
Given two tables A and B in a MySQL Database, how can one update
Given two tables, one for workers and one for tasks completed by workers, CREATE
Given two tables of equal structure, but one with 100M rows and another with
I am returning an xml result for a query. Given two tables: **Foo** FooId
Given two tables APPLE and ORANGE, NAME APPLES Alice 5 Bob 10 Trudy 1
Problem Given the following two tables, I'd like to select all Ids for Posts
Given a database with two tables X and Y , I have a query
I need to perform data validation of two tables using Selenium . Given a

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.