mysql update column with value from another table mysql update column with value from another table sql sql

mysql update column with value from another table


In addition to this answer if you need to change tableB.value according to tableA.value dynamically you can do for example:

UPDATE tableBINNER JOIN tableA ON tableB.name = tableA.nameSET tableB.value = IF(tableA.value > 0, tableA.value, tableB.value)WHERE tableA.name = 'Joe'


you need to join the two tables:

for instance you want to copy the value of name from tableA into tableB where they have the same ID

UPDATE tableB t1         INNER JOIN tableA t2              ON t1.id = t2.idSET t1.name = t2.name WHERE t2.name = 'Joe'

UPDATE 1

UPDATE tableB t1         INNER JOIN tableA t2              ON t1.id = t2.idSET t1.name = t2.name 

UPDATE 2

UPDATE tableB t1         INNER JOIN tableA t2              ON t1.name = t2.nameSET t1.value = t2.value


Second possibility is,

UPDATE TableB SET TableB.value = (    SELECT TableA.value     FROM TableA    WHERE TableA.name = TableB.name);