0
votes

I have 2 tables table1 (col1, col2, col3, col4) and table2 (çol1, col2, col5).

Table1 col3 is empty and i want to update data from table2 col5 based on match of table1(col1, col2 ) and table2 (col1, col2).

1

1 Answers

1
votes

For SQLite:

UPDATE table1
SET col3 = ( SELECT col5
             FROM table2
             WHERE (table1.col1, table1.col2)=(table2.col1, table2.col2) 
             LIMIT 1 )
/* WHERE table1.col3 IS NULL */

If (col1, col2) in table2 is defined as unique then LIMIT 1 may be removed.


For MySQL (the question is MySQL-tagged):

UPDATE table1
  JOIN table2 USING (col1, col2)
SET table1.col3 = table2.col5
/* WHERE table1.col3 IS NULL */

(col1, col2) in table2 must be defined as unique. If not then a random value from all possible will be used for updating.