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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:38:58+00:00 2026-06-02T06:38:58+00:00

I have 2 tables with the same columns. for example table 1 id name

  • 0

I have 2 tables with the same columns.
for example

table 1
id
name
height
weight

table 2
id
name
height
weight

The data is table 2 is complete. But in table 1, only some data exists, and the rest of the columns are empty. for example:

          table 1    table 2
id        4          4
name      (empty)    salman
height    5'11"      5'9" 
weight    (empyy)    65kg

I want a single script, that will allow me to update the table 2 with values from table 1, but only where it exists. In places where the table 1 is empty, I want to retain the data that already exists in table 2.

I’ve tried various ways, but all required multiple queries and are long and hectic. I want to know if there is a simpler way to get this done? Preferably in a single query?

Thank you

  • 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-02T06:38:59+00:00Added an answer on June 2, 2026 at 6:38 am

    You can try by joining the 2 tables and then using the CASE keyword to conditionally update the fields:

    UPDATE table2 t2 INNER JOIN table1 t1 USING (id)
    SET 
     t2.name = CASE WHEN (t1.name IS NULL) THEN t2.name ELSE t1.name END
     t2.height= CASE WHEN (t1.height IS NULL) THEN t2.height ELSE t1.height END
     t2.weight = CASE WHEN (t1.weight IS NULL) THEN t2.weight ELSE t1.weight END
    

    http://dev.mysql.com/doc/refman/5.5/en/case-statement.html

    http://dev.mysql.com/doc/refman/5.5/en/update.html

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

Sidebar

Related Questions

I have 3 database tables, all of them have the same 5 columns. They
I have a load of tables all with the same 2 datetime columns (lastModDate,
I have a table with multiple indexes, several of which duplicate the same columns:
I have two or more tables which hold values under the same column name.
I have two tables with the same structure: id name 1 Merry 2 Mike
I have two tables with exactly the same fields. Table A contains 7160 records
i have two data tables with the same structure the first one has one
If I have two tables in mysql that have similar columns... TABLEA id name
I have a table with four columns: Account#, Card#, CardNameType and Cardholder's Name. The
I have a (mysql) database table with the following columns: NAME | String (Unique)

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.