sql - 如何使用来自 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”输出
解决方案
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';
推荐阅读
- html - 悬停一个元素时如何更改其他元素
- javascript - 我正在尝试将带有 html 标签的 json 导入到我的 html 中。但它只是显示为巨大的字符串。我正在使用反应
- python - 逻辑回归sklearn中的参数“self”
- pandas - 如何比较具有相等值的列?
- c# - 如何使用 ASP.NET MVC C# Html 从视图中调用控制器中的函数
- javascript - 使用处理程序更新数组中的对象
- html - 如何将文字放在圆圈内?
- php - 在 HTML mailto 链接中使用 php
- php - 从 Ajax、FormData 发送图像并使用 PHP 接收的问题
- xampp-vm - 如何通过命令行启动 XAMPP-VM?