使用 postgreSQL 从另一个 table 更新 table 中的列

Updating a column in a table from another table using postgreSQL

我 table 命名为“城市”,其中 3 列命名为:

state, name, pop

还有一个 table cities1 有:

state, name

statename 对两个 table 都是通用的。我想将来自城市的 pop 列单独插入到 cities1 table.
中 如何使用 postgreSQL 完成此操作?

首先您需要在 cities1 table 中添加一个特定类型的列 pop,然后填写它。

ALTER TABLE cities1 ADD COLUMN pop [datatype here]

使用 cities 中的值更新 table cities1 中的 pop 列,其中 state,name 是常用列。

UPDATE cities1
SET pop = cities.pop
FROM cities
WHERE cities1.state = cities.state
    AND cities1.name = cities.name