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

  • Home
  • SEARCH
  • 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 need to update a group of cells by inserting the same two characters
I'm trying to do the classic Insert/Update scenario where I need to update existing
I need write an update statement that used multiple tables to determine which rows
I need to update a combobox with a new value so it changes the
I need to update a record in a database with the following fields [ID]
I need to update the comments field in a table for a large list
I need to update a field (which is currently empty) based on a match
So I need to update a text field. Neither the UPDATE statement or the
I have a table of about a million rows and I need to update
I have a Zimbra installation and I need to programmaticaly update contacts in it.

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.