gpt4 book ai didi

php - MySQL中如何将数据从一张表迁移到另一张表

转载 作者:行者123 更新时间:2023-11-29 00:23:01 25 4
gpt4 key购买 nike

我在两个不同的数据库中有两个相同的表(相同的表列和主键)。我想将第二个表数据添加到第一个表中不存在的第一个表中(根据主键)。最好的方法是什么?

我可以将第二个表数据导出为 csvphp arraysql 文件。

谢谢

最佳答案

有很多方法可以做到这一点。

最简单的可能就是这个one :

INSERT IGNORE
INTO table_1
SELECT *
FROM table_2
;

which allows those rows in table_1 to supersede those in table_2 that have a matching primary key, while still inserting rows with new primary keys.

或者,您可以使用子查询找出两个表不共享的行并插入它们。如果您有很多记录,您可能需要考虑使用临时表来加快该过程。

关于php - MySQL中如何将数据从一张表迁移到另一张表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20352564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com