【发布时间】:2018-04-28 01:30:57
【问题描述】:
我在网上花了很多时间试图寻找答案但没有成功,所以我决定发布以下内容。 我有以下表格:
TABLE_1
+------------+---------------+
| child_id | mother_id |
+------------+---------------+
| 1 | 4 |
| 2 | 3 |
| 3 | 2 |
| 4 | 1 |
+------------+---------------+
TABLE_2
+------------+---------------+
| child_id | mother_id |
+------------+---------------+
| 1 | 0 |
| 2 | 0 |
| 3 | 0 |
| 4 | 0 |
+------------+---------------+
我想用表 1 中的 mother_id 值更新表 2 中的 mother_id 值。 当然上面的例子可以手动解决:
UPDATE table2 SET mother_id = 1 where child_id = 4;
UPDATE table2 SET mother_id = 2 where child_id = 3;
UPDATE table2 SET mother_id = 3 where child_id = 2;
UPDATE table2 SET mother_id = 4 where child_id = 1;
但是让我们假设我有数千行要更新。有没有什么办法可以使用选择连接查询(在 child_id 上)与更新混合,以便只有几行代码?
谢谢。
【问题讨论】:
-
你想让 table2 匹配 table1?
-
是的,我想使用表 1 中的值更新表 2。
标签: mysql join sql-update