MYSQL;根据表A和表B的联接更新表A

时间:2018-12-15 10:52:46

标签: mysql sql

蝙蝠的权利,我要说的是,我相信这就是我想要的:

Update multiple rows using select statement。答案似乎是针对我想做的。

我的表如下。 tPatientsID仅包含两列:keyid和uid。

tEyeResults包含一个名为Patientid的列和一个名为puid的列。 Patientid的值与tPatientsIDs中的列uid匹配,而puid为空,这是最近通过更改表创建的。

我要做的就是将tuid的值puid设置为tPatientIDs中的keyid,该值与PatientID列的值相对应。

这是我基于SO答案使用的查询:

UPDATE tEyeResults SET puid = tPatientIDs.keyid FROM tPatientIDs WHERE 
tPatientIDs.uid = tEyeResults.patientid; 

即使结构似乎与答案相同,我也会收到错误:

You have an error in your SQL syntax; check the manual that corresponds to 
your MySQL server version for the right syntax to use near 'FROM 
tPatientIDs 
WHERE tPatientIDs.uid = tEyeResults.patientid' at line 1

1 个答案:

答案 0 :(得分:1)

在更新中使用JOIN

UPDATE tEyeResults e 
JOIN tPatientIDs p ON p.keyId = e.patientId
SET e.puid = tPatientIDs.keyid 

也许您需要一个where子句来仅更新一名患者,但您的问题中我没有看到