Reputation: 37
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, I 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
I can not use
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?
Upvotes: 0
Views: 424
Reputation: 222462
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
I think you want update
, not 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
; when found, it updates columns f
, g
and h
of table2
with values of column a
, b
, c
of table1
.
For this to work consistently, a
should be a unique key in both tables.
Upvotes: 2