当前位置:主页 > 数据库 > PostgreSQL >

postgresql 两表关联更新操作

时间:2022-03-27 08:31:14 | 栏目:PostgreSQL | 点击:

我就废话不多说了,大家还是直接看代码吧~

UPDATE 要更新的表
SET 字段1 = cqt.字段1,
 字段2 = cqt.字段2,
FROM
 数据来源表 cqt
WHERE
 要更新的表.bsm = cqt.bsm

补充:Postgresql 连表更新字段语法 update

下面这段sql本来目的是想更新条件下的数据,可是这段sql却更新了整个表的数据。sql如下:

UPDATE tops_visa.visa_order
SET op_audit_abort_pass_date = now()
FROM
 tops_visa.visa_order as t1
INNER JOIN tops_visa.visa_visitor as t2 
ON t1. ID = t2.order_id
WHERE
 t1.op_audit_abort_pass_date IS NULL
AND (
 t2. STATE = 'pch_abort_op_audit_pass'
 OR t2. STATE = 'pvd_abort_op_audit_pass'
)
 

正确的写法有多种,如下方法也能够实现正确的数据库修改:

UPDATE tops_visa.visa_order as t1
SET op_audit_abort_pass_date = now()
FROM
 tops_visa.visa_visitor AS t2
WHERE
 t1.ID = t2.order_id
AND t1.op_audit_abort_pass_date IS NULL
AND (
 t2. STATE = 'pch_abort_op_audit_pass'
 OR t2. STATE = 'pvd_abort_op_audit_pass'
)

您可能感兴趣的文章:

相关文章