【发布时间】:2020-05-23 04:36:59
【问题描述】:
我有 2 个表 table1 (col1, col2, col3, col4) 和 table2 (çol1, col2, col5)。
Table1 col3 为空,我想根据 table1(col1, col2) 和 table2 (col1, col2) 的匹配更新 table2 col5 中的数据。
【问题讨论】:
我有 2 个表 table1 (col1, col2, col3, col4) 和 table2 (çol1, col2, col5)。
Table1 col3 为空,我想根据 table1(col1, col2) 和 table2 (col1, col2) 的匹配更新 table2 col5 中的数据。
【问题讨论】:
对于 SQLite:
UPDATE table1
SET col3 = ( SELECT col5
FROM table2
WHERE (table1.col1, table1.col2)=(table2.col1, table2.col2)
LIMIT 1 )
/* WHERE table1.col3 IS NULL */
如果table2 中的(col1, col2) 被定义为唯一,则可以删除LIMIT 1。
对于 MySQL(问题是 MySQL 标记的):
UPDATE table1
JOIN table2 USING (col1, col2)
SET table1.col3 = table2.col5
/* WHERE table1.col3 IS NULL */
table2 中的(col1, col2) 必须定义为唯一的。如果不是,那么将使用所有可能的随机值进行更新。
【讨论】: