2

我有一个 table_a,我想用其他 2 个表中的数据进行更新。

table_a
+----+---------+------------+-------+
| id |  user   |    date    | valid | 
+----+---------+------------+-------+
| 10 | Bob     |            |       |
| 11 | Joe     |            |       |
| 12 | Joe     |            |       |
| 13 | Pete    |            |       |
| 14 | Bob     |            |       | 
+----+---------+------------+-------+

日期来自 table_b 其中 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 |        
+----+----------+----------+

有效来自 table_c 其中 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    |        
+----+----------+----------+

如何使用 SQL 查询来做到这一点?

4

1 回答 1

4

您可以简单地使用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
于 2013-04-05T14:37:32.343 回答