首页 > 解决方案 > 如何使用来自 2 个表 SQL 查询的数据更改表中的数据

问题描述

我有 2 个表(“users”和“user_workplace”)。表“用户”具有列 id、first_name、last_name。表“user_workplace”有 id、user_id(user_workplace.user_id = users.id)、status(DISABLED 或 ACTIVE)。

我有用户的名字和姓氏,我需要在表“user_workplace”中将状态更新为“禁用”。

我怎么能这样做?

我已经尝试过类似的方法,但是出现了错误(错误:缺少表“用户”第 2 行的 FROM 子句条目:WHERE user_workplace.user_id = users.id 和):

UPDATE user_workplace SET status = 'DISABLED'
WHERE user_workplace.user_id = users.id and 
users.first_name like 'John ' and 
users.last_name like 'Doe';

和这个:

UPDATE
    public.user_workplace 
SET
    status = 'DISABLED'
FROM
    public.users AS Table_u,
    public.user_workplace AS Table_uw
    
WHERE
    Table_uw.user_id = Table_u.id and 
    Table_u.first_name like 'John ' and 
    Table_u.last_name like 'Doe';

有“UPDATE 0”输出

标签: sqldatabasepostgresqljoin

解决方案


UPDATE user_workplace 
SET status = 'DISABLED'
from users
WHERE user_workplace.user_id = users.id and 
users.first_name like 'John ' and 
users.last_name like 'Doe';

推荐阅读