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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:12:38+00:00 2026-05-14T01:12:38+00:00

I need to update two tables inside a single transaction. The individual queries look

  • 0

I need to update two tables inside a single transaction. The individual queries look something like this:

1. INSERT INTO t1 (col1, col2) 
   VALUES (val1, val2) 
   ON DUPLICATE KEY 
      UPDATE col2 = val2;

If the above query causes an insert then I need to run the following statement on the second table:

2. INSERT INTO t2 (col1, col2) 
   VALUES (val1, val2) 
   ON DUPLICATE KEY  
      UPDATE col2 = col2 + val2;

otherwise,

3. UPDATE t2 
       SET col2 = col2 - old_val2 + val2 
     WHERE col1 = val1; 
   -- old_val2 is the value of 
      t1.col2 before it was updated

Right now I run a SELECT on t1 first, to determine whether statement 1 will cause an insert or update on t1. Then I run statement 1 and either of 2 and 3 inside a transaction. What are the ways in which I can do all of these inside one transaction itself?

The approach I was thinking of is the following:

UPDATE t2, t1 
   set t2.col2 = t2.col2 - t1.col2 
 WHERE t1.col1 = t2.col2 
   and t1.col1 = val1;

INSERT INTO t1 (col1, col2) 
VALUES (val1, val2) 
ON DUPLICATE KEY 
   UPDATE col2 = val2;

INSERT INTO t2, t1 (t2.col1, t2.col2) 
VALUES (t1.col1, t1.col2) 
ON DUPLICATE KEY 
   UPDATE t2.col2 = t2.col2 + t1.col2 
WHERE t1.col1 = t2.col2 
  and t1.col1 = val1;

Unfortunately, there’s no multi-table INSERT… ON DUPLICATE KEY UPDATE in MySQL 5.0. What else could I do?

  • 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-05-14T01:12:39+00:00Added an answer on May 14, 2026 at 1:12 am

    Okay, so I’ve got this figured out and done in a way that I like it:

     UPDATE t2, t1 
       SET t2.col2 = t2.col2 - t1.col2 
     WHERE t1.col1 = t2.col2 
       AND t1.col1 = val1;
    
    INSERT INTO t1 (col1, col2) VALUES (val1, val2) 
    ON DUPLICATE KEY UPDATE
       col2 = val2;
    
    INSERT INTO t2 (col1, col2) VALUES (val1, val2) 
    ON DUPLICATE KEY UPDATE
       col2 = col2 + VALUES(col2);
    

    The third query can be rewritten to refer to values from t1 like such:

    INSERT INTO t2 (col1, col2)
       SELECT col1, col2 FROM t1 WHERE col1 = val1
    ON DUPLICATE KEY UPDATE
       t2.col2 = t2.col2 + VALUES(col2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using SqlDataAdapter.Update with DataTables to update two SQL tables in a single transaction.
I have two tables with the same ID column PK/FK. I need to update
I need to update two tables in MySQL with PHP. The second table needs
I need to update some information which are stored in two tables. To update
I have two tables involved in this query I need to create, and I'm
I need to turn this query into an update statement. I will have to
I have two tables that I designed this way with a possible reshuffling of
Imagine I have two tables in a DB like so: products: product_id name ----------------
I have two datagridview inside a winform. I need to reload with a button
I need to set up a queue system using some SQL tables, like the

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.