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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:24:31+00:00 2026-06-03T16:24:31+00:00

Table 1: ID Total1 Total2 Total3 1 10 0 20 1 40 0 50

  • 0

Table 1:

ID      Total1 Total2 Total3
1         10       0          20
1         40       0          50
1         15       0          5

Table 2:

ID     Total

1        140

How do i add cumulatively all the rows and find the matching total in table2?

How do i add cumulatively all the rows and find the NOT matching total in table2 and write update statement to update (add those) and write to table2 ?

select sum(t1.total1) + sum(t1.total2) +sum(t1.total3) as "Total" from
table t1, table t2 where t1.ID=t2.ID and  sum(t1.total1) +
sum(t1.total2) +sum(t1.total3)=t2.total;

I am not able to get it correctly the update syntax.

  • 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-03T16:24:33+00:00Added an answer on June 3, 2026 at 4:24 pm

    Looks like you have the right query to find the totals and match them up. To update rows that don’t have the same total, you could use a correlated subquery like this:

    UPDATE table2
    SET total = (
       SELECT SUM(Total1 + Total2 + Total3) FROM table1
       WHERE id = table2.id
       GROUP BY id
    )
    

    Or with a join (slightly more complicated, but likely faster)

    UPDATE t2
    SET t2.total = t1.total
    FROM table2 t2
    INNER JOIN (
       SELECT id, SUM(Total1 + Total2 + Total3) as total FROM table1
       GROUP BY id
    ) t1 ON t1.id = t2.id
    WHERE t2.total <> t1.total
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with total no of 1000 records in it.It has the
Example table content 'main' | id | total | | 1 | 10 |
...besides the fact that I am a total amateur? My table is set up
I have a table (dump) with transactions, and I want to list the total
I have one datatable. In that table I have columns named [Total Day], [Present
let's say that I have a table called Items (ID int, Done int, Total
Ok, so i have one really monstrous MySQL table (900k records, 180 MB total),
For example, if we have a table Books, how would we count total number
Suppose you had a table of Cars with hundreds of thousands of rows, and
This is my table definition: CREATE TABLE orders_total ( orders_total_id int unsigned NOT NULL

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.