简体   繁体   English

如果来自不同表的两个ID匹配,MySQL将特定值插入列中?

[英]MySQL insert specific value into column if two id from different tables match?

I have 3 tables employees , managers , users . 我有3个表的员工经理用户 I want to insert value 1 into users column is_manager only if the id from employees matches id from managers . 我仅在雇员的 id管理者ID匹配时才将值1插入到用户is_manager中 id is primary key in employees and primary and foreign key in managers . id员工的主键, 经理的主键和外键。

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks 谢谢

Either

UPDATE users 
INNER JOIN managers ON users.id=managers.id
SET users.is_manager=1

or 要么

UPDATE users SET is_manager=1
WHERE id IN (
  SELECT id FROM managers
)

will do. 会做。 I recommend the latter for better readability. 我建议使用后者以提高可读性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 mysql从第一个插入两个表ID - mysql insert into two tables id from first 比较来自两个不同mysql表的数据插入新数据并更新不匹配的数据 - comparing data from two different mysql tables insert new data and update the data that don't match 匹配来自两个不同表的两个不同列 - match two different column from two different tables 如何使用mysql中的特定ID从两个不同的表中选择行? - How to select row from two different tables using specific id in mysql? 如果在两个不同的表中没有匹配项,则SQL查询返回列的默认值 - SQL query to return default value for a column if no Match in two different tables SELECT 来自两个表 WHERE 每个表中的不同列等于 $id ORDER BY 公共列(PHP / MySQL) - SELECT from two tables WHERE different columns in each table equal $id ORDER BY common column (PHP/MySQL) 从两个mysql表中选择,它们的列值相似并且id从上一页检索 - select from two mysql tables where column value is similar and id is retrieved from previous page mysql连接两个表以获取某些列是特定值的地方 - mysql joining two tables to get where certain column is a specific value MySQL复制列来自不同的表,插入表的顶部 - MySQL copy column from different tables, insert at top of table 如何从不同的MySQL表获取特定的ID - How to get specific id's from different MySQL Tables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM