将不存在的行从 column2 插入 column1

Inserting into column1 from column2 just the rows which do not exist already

我在不同的 table 中有两列。我经常需要根据第二列的值更新一列。那么如何才能将 新值插入到我的列中?

Table A 有 col1 table B 有 col2:

A 的值:1 2 3

B 的值:1 2 3 4

我需要更新 A.col1 以便其中包含值 4。

我不完全理解这个问题。我想你是在问你如何从 A.col1 插入到 B.col2 中,而 B.col2 还没有价值?在那种情况下:

update table B set B.col2 = A.col1 from A where B.someId = A.someId and B.col2 is null;

这将只在 table A.col1

中添加值为 4 的行

INSERT INTO A(col1) SELECT col2 FROM B WHERE col2 NOT IN (SELECT col1 FROM A);

谢谢你的帮助