我有一个使用userid作为主键的成员资格表。
我有另一个表(表A),其中包含两个到membership(tableA.userid1和tableA.userid2)的外键。
现在我想使用userid1的电子邮件更新userid2的成员资格表中的电子邮件地址。
我该怎么做?有点像...
Update membership m2
set m2.email = m1.email
...???
答案 0 :(得分:2)
UPDATE m2
SET email=m1.email
FROM TableA ta
INNER JOIN membership m1 ON ta.UserID1=m1.UserID
INNER JOIN membership m2 ON ta.UserID2=m2.UserID
答案 1 :(得分:1)
update A
join membership as m1 on A.userid1 = m1.id
join membership as m2 on A.userid2 = m2.id
set m2.email = m1.email
,其中
create table membership (
id int primary key auto_increment,
email varchar(100)
);
create table A (
userId1 int,
userId2 int,
constraint foreign key (userId1) references membership (id),
constraint foreign key (userId2) references membership (id)
);
insert into membership (email) values
("user1@stackoverflow.com"),
("user2@stackoverflow.com");
insert into A values (1, 2);
update命令将用户2的电子邮件设置为用户1的电子邮件:
select * from membership;
id email
1 user1@stackoverflow.com
2 user1@stackoverflow.com
以下是SQL Fiddle
的链接