Reputation: 366
I have a table_a that I want to update with data from 2 other tables.
table_a
+----+---------+------------+-------+
| id | user | date | valid |
+----+---------+------------+-------+
| 10 | Bob | | |
| 11 | Joe | | |
| 12 | Joe | | |
| 13 | Pete | | |
| 14 | Bob | | |
+----+---------+------------+-------+
the date comes from table_b where table_b.rel_id = table_a.id
table_b
+----+----------+----------+
| id | rel_id | date |
+----+----------+----------+
| 30 | 8 | 10/10/11 |
| 31 | 9 | 10/10/11 |
| 32 | 10 | 10/10/11 |
| 33 | 11 | 10/10/11 |
| 34 | 12 | 10/10/11 |
| 35 | 13 | 10/10/11 |
| 36 | 14 | 10/10/11 |
+----+----------+----------+
and valid comes from table_c where table_c.rel_id = table_a.id
table_c
+----+----------+----------+
| id | rel_id | valid |
+----+----------+----------+
| 40 | 10 | yes |
| 41 | 11 | no |
| 42 | 12 | yes |
| 43 | 13 | no |
| 44 | 14 | yes |
| 45 | 15 | no |
| 46 | 16 | yes |
+----+----------+----------+
How can this be done with a SQL-Query?
Upvotes: 2
Views: 1378
Reputation: 263703
You can simply join the tables using INNER JOIN
.
UPDATE table_a a
INNER JOIN table_b b
ON a.id = b.rel_id
INNER JOIN table_c c
ON a.id = c.rel_id
SET a.date = b.date,
a.valid = c.valid
Upvotes: 4