H. Ferrence
H. Ferrence

Reputation: 8106

How to Update MYSQL Table Column Based on Value in Related Table

I have a Table A as follows:

ID NAME VALUE
1  abc  10
2  xyz  15

I have a TABLE B as follows:

ID VALUE_1 VALUE_2 VALUE_3 TOTAL YEAR
1  0       0       0       0     2012
2  0       0       0       0     2013
3  0       0       0       0     2012

I want to UPDATE all rows in Table B and SET VALUE_2 column to the VALUE in Table A

I started my query statement as follows:

$query_string = '
UPDATE Table_B
SET VALUE_2 = (SELECT...something should go here I think)
WHERE Table_B.year = "2013"

Thank you for helping

Upvotes: 1

Views: 783

Answers (1)

John Woo
John Woo

Reputation: 263703

UPDATE  tableB b
        INNER JOIN tableA a
            ON a.ID = b.ID AND b.YEar = 2013
SET     b.VALUE_2 = a.Name

Upvotes: 2

Related Questions