[英]in MySQL, how to use information from one table to update another table
I have two tables: teams, and persons. 我有两个表:团队和人员。
table teams
have three columns, id
, name
, leader
表
teams
有三列, id
, name
, leader
table persons
have these columns: hash
, team_id
表
persons
具有以下列: hash
, team_id
teams.leader is a MD5 hash that must match persons.hash in order to determine which person is leader of a given team. team.leader是必须与person.hash匹配的MD5哈希,以确定哪个人是给定团队的负责人。
I need to run a query on MySQL that does the following: 我需要在MySQL上执行以下操作的查询:
1) Retrieve all the leaders of a team, and the team id: 1)检索团队的所有领导者,以及团队ID:
SELECT `id`,`leader` FROM `teams`;
2) Use such information to update team_id
on table persons
2)使用此类信息来更新表
persons
team_id
This is my current Query: SELECT id
FROM teams
INNER JOIN persons
ON teams
. 这是我当前的查询:SELECT
id
FROM teams
INNER JOIN persons
在teams
。 leader
= persons
. leader
= persons
。 hash
but I haven't been able to come up with a solution that allows me update column team_id
with the corresponding leader. 但我无法提出一种解决方案,该解决方案不允许我用相应的领导者更新列
team_id
。 I've been thinking probably using cursors, but not sure about it. 我一直在考虑可能使用游标,但不确定。 Any ideas?
有任何想法吗?
You can use the multiple table UPDATE
syntax to join the tables: 您可以使用多表
UPDATE
语法来联接表:
UPDATE teams JOIN persons ON teams.leader = persons.hash
SET persons.team_id = teams.id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.