You need to unite the two by the common key of both:
UPDATE tabela1
INNER JOIN tabela2 ON tabela1.id = tabela2.id
SET tabela1.dado1 = tabela1.dado1 + tabela2.dado2;
See working on SQL Fiddle.
Important to know that in the clause ON
the condition joining the two tables must be specified. Note that the fields have different names in each table, not to invert in the ON
or forget to adjust any of the sides.
If you want, before you touch the table, test the ON
with a SELECT
to see if everything is in order before modifying the data:
SELECT *
FROM tabela1
INNER JOIN tabela2 ON tabela1.id = tabela2.id;
We chose INNER JOIN
why are we updating the tabela1
with data from tabela2
, then we are not interested in lines where there is no match between the tables.
Important: We are assuming that the relationship is 1:1. If you have repeated Ids in any of the tables, the value will be added more than once (but then it’s an architecture decision, not a problem in query specifically).
To better understand which JOIN
use in each case, see this post:
What is the difference between INNER JOIN and OUTER JOIN?
Start by posting here structure of tables
– Marco Souza
without seeing the table structure is difficult, but tries something like UPDATE Tabela1, table2 SET Tabela1.date1 = Tabela1.date1 + table2.date2
– Hugo Leonardo