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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:56:30+00:00 2026-05-28T04:56:30+00:00

I need to copy data from one table to another. The tables do not

  • 0

I need to copy data from one table to another. The tables do not have all the same columns, or order; but the data to be copied is always in the same columns; that is data from column foo should be copied to columns foo.

If it was only two tables I could just hardcode the column names like:

INSERT INTO table_target ( column1, column2, column4 ) 
  SELECT column1, column2, column4 FROM table_source;

However there are a couple dozen tables, and some extra transformation needs to be done, so it would be nice if I could just say: Copy any matching columns and ignore the rest.

I’ve managed to figure out how to get a list of the common columns, but now I’m stuck.

SELECT src.col
  FROM (SELECT COLUMN_NAME as col
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE table_name = 'table_target') as trg
INNER JOIN 
  (SELECT COLUMN_NAME as col
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE table_name = 'table_source') as src ON (src.col=trg.col)
; 
  • 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-28T04:56:30+00:00Added an answer on May 28, 2026 at 4:56 am

    I trick I have used in the past to good effect is to write a query that returns SQL, then just copy-paste it into the db command shell. In this case, this would be your query:

    SELECT CONCAT(
        'INSERT INTO table_target (',  
        GROUP_CONCAT(trg.col), -- produces output like "col1, col2, col3"
        ') SELECT ',
        GROUP_CONCAT(trg.col), -- produces output like "col1, col2, col3"
        ' FROM table_source;') as sql_stmt
    FROM (
    (SELECT COLUMN_NAME as col
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE table_name = 'table_target') as trg
    INNER JOIN 
    (SELECT COLUMN_NAME as col
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE table_name = 'table_source') as src ON src.col=trg.col) x;
    

    This makes use of mysql’s handy GROUP_CONCAT function that aggregates the value into a CSV – perfect for creating a list of column names for generating SQL

    I also wrapped your query in an alias so we can select from it.

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

Sidebar

Related Questions

I need to copy some data from one table to another but I need
I need to copy some data from one table to another in Oracle, while
when i copy data from one table to another all the primary key values
I need to copy a set of data from one table to another that
I need to copy several tables from one DB to another in SQL Server
I need to copy the data from one database to another only if the
Using C# (vs2005) I need to copy a table from one database to another.
I need to copy data from one database to another with EF. E.g. I
I need to copy data from one database to another using a VB.NET program.
I need to copy specific data from select set of tables in one SQL

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.