I’m having a problem with MySQL
I have two tables, I want to store Val1 from Table 2 into Val2 in Table 1
Table1
ID-----Val1-----Val2
1------A--------NULL
2------B--------NULL
3------C--------NULL
4------D--------NULL
5------E--------NULL
Table2
ID-----Val1
1------aaa
2------bbb
3------ccc
4------ddd
5------eee
So that Table1 now looks like
Table1
ID-----Val1-----Val2
1------A--------aaa
2------B--------bbb
3------C--------ccc
4------D--------ddd
5------E--------eee
Right now, I have
INSERT INTO Table1(Val2) SELECT Val1 FROM Table2 WHERE Table1.ID=Table2.ID;
Any help is appreciated!
You can use either a subquery (SQLize):
or a multi-table update (SQLize):
or the same with an explicit
JOIN(SQLize):(I assume you only want to update the rows in
Table1for whichVal2is NULL. If you’d rather overwrite the values for all rows with matchingIDs inTable2, just remove theWHERE Table1.Val2 IS NULLcondition.)