In MySQL, how to copy the content of one table to another table within the same database?

后端 未结 8 1285
既然无缘
既然无缘 2020-12-22 16:40

I am new to MySQL. I would like to copy the content of one table to another table within the same database. Basically, I would like to insert to a table from another table.

相关标签:
8条回答
  • 2020-12-22 17:23

    INSERT INTO TARGET_TABLE SELECT * FROM SOURCE_TABLE;

    EDIT: or if the tables have different structures you can also:

    INSERT INTO TARGET_TABLE (`col1`,`col2`) SELECT `col1`,`col2` FROM SOURCE_TABLE;
    

    EDIT: to constrain this..

    INSERT INTO TARGET_TABLE (`col1_`,`col2_`) SELECT `col1`,`col2` FROM SOURCE_TABLE WHERE `foo`=1
    
    0 讨论(0)
  • 2020-12-22 17:25

    This worked for me. You can make the SELECT statement more complex, with WHERE and LIMIT clauses.

    First duplicate your large table (without the data), run the following query, and then truncate the larger table.

    INSERT INTO table_small (SELECT * FROM table_large WHERE column = 'value' LIMIT 100)
    

    Super simple. :-)

    0 讨论(0)
提交回复
热议问题