Search code examples
mysqlalter-tablealter

Is it possible to append the columns (structure and content) of one mysql table to another?


I would like to copy the structure and the content of one mysql table to another, adding all of the columns and values of that table to the already existing ones in the other table.

I could do it manually, but since I'm talking about a large amount of columns, it would be great if there were some sort of ALTER statement to help me do that.

EDIT:

To explain myself better:

I first need to add the columns contained in table B (column_name, data_type) to table A (which already has its own set of columns). Once that is done, I can copy the content, which is easy.

I guess the real question is: is there a way to add the columns contained in table B to another table (table A) which has columns of its own?


Solution

  • To build on flavianatill's second solution, it seems to me that the export/import step is not needed. If I understand the problem correctly, the following one-liner should do it.

    CREATE TABLE IF NOT EXISTS merged_table AS (SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id);
    

    Sorry, I would have put this in a comment but I lack the reputation!