[英]insert data from one table into another using a condition mysql
hello I have the following problem I have two tables let's say these are table 1 and table 2. Table 1 has column A, B, C, D and table 2 has column A, B, C, D, E, F, G, H, II need to insert data B, C, D from table 1 in table 2 in columns F, G, H using as Condition that A = A你好,我有以下问题,我有两个表,假设它们是表 1 和表 2。表 1 有 A、B、C、D 列,表 2 有 A、B、C、D、E、F、G 列, H, II 需要将表 1 中的数据 B, C, D 插入到表 2 的 F, G, H 列中,条件为 A = A
I can not use我不能用
INSERT INTO table2 (F, G, H) SELECT B, C, D FROM table1;
INSERT INTO table2 (F, G, H) SELECT B, C, D FROM table1;
since the data is out of order so when inserting the data they would be mixed因为数据是乱序的,所以在插入数据时它们会被混合
Is it possible to make a sentence that complies with the request, and if it complies, in what way would it be?是否可以造出一个符合要求的句子,如果符合,会是什么方式?
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
我需要插入数据 B、C、D 从表 1 到表 2 的 F、G、H 列中使用 A = A 的条件
I think you want update
, not insert
:我想你想要
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
For each row in table2
, this searches for a match in table1
with a match on a
;对于
table2
中的每一行,这会在table1
中搜索与 a 上a
匹配项的匹配项; when found, it updates columns f
, g
and h
of table2
with values of column a
, b
, c
of table1
.找到后,它会使用
table1
的a
、 b
、 c
列的值更新table2
的f
、 g
和h
列。
For this to work consistently, a
should be a unique key in both tables.为了使其始终如一地工作,
a
应该是两个表中的唯一键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.