mysql更新kunde和profil的多表

dgtucam1  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(175)

我有一张table“profil”和“kunde”profil“收到了邮件和密码。”kunde“从profil获得了个人简历、用户名和电子邮件作为索引。”“kunde”得到了主键用户名。我想马上更新用户名bio和passwort UPDATE profil, kunde SET kunde.username = ?, kunde.bio = ?, profil.passwort = ? WHERE profil.email = ?; 我得到的错误码 #1062 - duplicated entry 'Kneipengänger60' for key 'PRIMARY'

qpgpyjmq

qpgpyjmq1#

不能在一条语句中更新两个表。但是,您可以使用事务来确保这两个语句作为单个单元进行更新。如果一个失败,两个都将回滚。

BEGIN TRANSACTION;

UPDATE profil
SET profil.passwort = ?
FROM profil, kunde
WHERE profil.id = kunde.id //Here you use the secondary key and primary keys to link the tables
and profil.email = ?;

UPDATE kunde
SET kunde.username = ?, kunde.bio = ?
FROM profil, kunde
WHERE profil.id = kunde.id //Here you use the secondary key and primary keys to link the tables
and profil.email = ?;

COMMIT;

相关问题