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'
)

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。如有錯誤或未考慮完全的地方,望不吝賜教。

推薦閱讀: