sql - 根据另一个表中的值更新 2 列

标签 sql postgresql

我有 2 个表,我想写 2 个更新。
tbl_stage with id, flag (null) and count(null) column.
带有 ID、计数、更新列的 tbl_source。

更新前

tbl_stage                 tbl_source
id    flag    count       id    count    updated
abc   NULL    NULL        abc   9.0      false
                          def   3.6      false

我想更新 tbl_stage.count=tbl_source.count 和 tbl_stage.flag = true 其中 tbl_stage.id=tbl_source.id。
另一个,tbl_source.updated = true 其中 tbl_stage.id=tbl_source.id。

更新后应该是这样的。

tbl_stage                 tbl_source
id    flag    count       id    count    updated
abc   true    9.0         abc   9.0      true
                          def   3.6      false

最佳答案

您肯定需要使用 2 个 UPDATE 语句,因为在一个语句中更新 2 个表是不可用的。

update tbl_stage
set [count]=tbl_source.count 
    ,flag = 1
FROM tbl_source WHERE tbl_stage.id=tbl_source.id

update tbl_source
set updated = 1
from tbl_source WHERE tbl_stage.id=tbl_source.id
and tbl_stage.count=tbl_source.count

关于sql - 根据另一个表中的值更新 2 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39295548/

相关文章:

sql - 在 Postgres 中包含模式信息的主键

sql - 选择查询并使用 SQL 中的 select 进行插入

python - 如何使用 DATABASE_URL 将 Docker 容器连接到在本地主机上运行的 PostgreSQL?

sql - Oracle 中的 OVER 子句

python - 在多处理代码中使用 pyodbc

sql - 按其他列的特定规则选择行值对

django - 破译 psycopg2 :OperationalError 的特例

sql - 在不使用子查询的情况下从同一个表中的正则表达式匹配更新

sql - 如何确定一个时间戳在 postgresql 中是否在另一个时间戳之前?

php - 无法使用 brew 找到 PHP 的 php72-pdo-pgsql/PostgreSQL 驱动程序