Select & Insert across multiple databases with MySQL

前端 未结 2 2161
生来不讨喜
生来不讨喜 2021-02-07 07:00

I have 2 identical tables in 2 different databases that reside on the same server. What would be the best way to copy data from table to another?

2条回答
  •  北海茫月
    2021-02-07 07:36

    Use:

    INSERT INTO db1.table1
    SELECT *
      FROM db2.table2 t2
     WHERE NOT EXISTS(SELECT NULL
                        FROM db1.table1 t1
                       WHERE t1.col = t2.col)
    

    The exists is simplified, but you left out if there's a primary key/auto_increment to worry about/etc.

提交回复
热议问题