Reputation: 273
I have a table:
Table1:
Id value
1 10
2 20
3 30
4 40
In table 2:
Id
1
1
2
2
3
4
4
4
I want to get the results:
Table2:
Id value
1 10
1 10
2 20
2 20
3 30
4 40
4 40
4 40
I know add a column with following code:
ALTER table Table2 ADD value int(11)
How to fill the data??
Upvotes: 4
Views: 7004
Reputation: 18479
you need to first ALTER the table and then fill the values using JOIN command as:
ALTER table Table2 ADD value int(11);
Then insert / fill the values as:
UPDATE Table2 INNER JOIN Table1
ON Table2.id = Table1.id
SET Table2.value = Table1.value
Upvotes: 3
Reputation: 657
You could run this update command juste after tours.
UPDATE Table2 SET value = (SELECT value FROM Table1 WHERE Table2.Id = Table1.Id)
Upvotes: 0
Reputation: 2154
I think these SQL useful to you.
UPDATE Table2 LEFT JOIN Table1 ON Table2.id = Table1.id
SET Table2.value = Table1.value
Thank you.
Upvotes: 3