简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM