使用条件 mysql 将数据从一个 table 插入另一个

insert data from one table into another using a condition mysql

你好我有以下问题我有两个 tables 假设它们是 table 1 和 table 2。 Table 1 有 A、B、C、D 列 table 2 有 A、B、C、D、E、F、G、H、I 列 我需要在 F、G、H 列的 table 1 in table 2 中插入数据 B、C、D,条件是 A = A

我不会用

INSERT INTO table2 (F, G, H) SELECT B, C, D FROM table1;

因为数据是乱序的所以插入数据的时候会混在一起[=13​​=]

是否可以造出符合要求的句子,如果符合,会是什么方式?

I need to insert data B, C, D from table 1 in table 2 in columns F, G, H using as Condition that A = A

我想你想要 update,而不是 insert:

update table2 t2
inner join table1 t1 on t1.a = t2.a
set t2.f = t1.b, t2.g = t1.c, t2.h = t1.d

对于 table2 中的每一行,这将搜索 table1 中的匹配项和 a 中的匹配项;找到后,它会使用 abc 列的值更新 table2fgh 列共 table1.

为了使其始终如一地工作,a 应该是两个表中的唯一键。