如何根据另一个 table 中的值更新 table?

How to update a table based on the values in another table?

我正在尝试使用另一个 table 的列中包含的值更新一个 table 中的列。 我已经尝试了以下但它给了我一个错误说它 returns 多于一行。

update Table1 set description1 = (select description2 from Table2)
where customer_id in (select customer_id from Table2);    

有什么指导吗?

要进行相关更新,您的子查询必须 return 一行。几乎总是,您通过关联告诉您 table2 中的哪一行映射到 table1 中的哪一行的键来执行此操作。假设两个表都有一个名为 key 的列,该列是唯一的

UPDATE table1 t1
   SET description = (SELECT t2.description2
                        FROM table2 t2
                       WHERE t1.key = t2.key)
 WHERE t1.customer_id IN (SELECT t2.customer_id
                            FROM table2)

似乎 select description2 from Table2 返回了不止一行。 由于 Oracle 不知道要准确更新到哪个值 (description)。

如果要将所有值更新为相同的值 (description),请使用 WHERE CLAUSE 并将行过滤为 1。如果查询 [=10=,请检查和 运行 ] returns只有1行。

否则按照 Justin Cave 的建议使用密钥进行相关更新。