gpt4 book ai didi

mysql - 我们可以在 1 个查询 postgresql 中更新两个表吗

转载 作者:行者123 更新时间:2023-11-29 11:15:22 25 4
gpt4 key购买 nike

我使用 postgres 作为数据库并有 2 个表

accounts
id | name | status


accountOwner
account_id | user_id

我有 user_id(accountOwner 表)和 status(帐户表)要根据我拥有的 accountid 进行更新。是否可以在 1 个查询中更新两个表?我尝试了以下方法

UPDATE accounts,accountOwner SET accounts.status='active', accountOwner.user_id=3 WHERE  accounts.id=accountOwner.account_id AND accountOwner.account_id = 1;

最佳答案

不,您不能同时更新两个表。 The documentation说:

UPDATE -- update rows of a table

但是,您可以使用 FROM 子句,例如:

UPDATE accounts SET status = 'active'
FROM accountOwners
WHERE accounts.id = accountOwners.account_id
AND accountOwners.account_id = 1;

关于mysql - 我们可以在 1 个查询 postgresql 中更新两个表吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39844794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com