Answer the question
In order to leave comments, you need to log in
How to merge 2 tables so that one overlaps part of the other?
There are two tables with the same structure - 2 columns: 'id' and 'activity'. 'id' field values are unique. The first table lists all possible ids (the second table cannot have an id that is not in the first table). All the 'activity' fields in the first table contain 0. The second table contains no more rows than the first table, and the 'activity' fields of the second table contain some numbers other than zero. It is necessary to combine these two tables in such a way as to overlap part of the first table with the second table, so that the resulting table has the same number of rows as in the first table, and for those rows whose id is present in the second table, the 'activity' field contained the value from the second tables.
Example
First table:
id activity
1 0
2 0
3 0
4 0
id activity
2 17
4 45
id activity
1 0
2 17
3 0
4 45
Answer the question
In order to leave comments, you need to log in
SELECT
table1.id,
IFNULL(table2.activity, table1activity) as activity
FROM table1
LEFT JOIN table2 ON table1.id = table2.id
UPDATE T1
SET t1.activity = t2.activity
FROM table1 T1
INNER JOIN table2 T2
on t1.id = t2.id
SELECT id, CASE WHEN t1.activity = 0 THEN t2.activity ELSE t1.activity END as activity
FROM t1
LEFT JOIN t2 ON t1.id = t2.id
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question