简体   繁体   English

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

[英]MYSQL; Update Table A base on join from Table A and Table B

Right of the bat, I'm going to say that I believe that this is what I want: 蝙蝠的右边,我要说的是,我相信这就是我想要的:

Update multiple rows using select statement . 使用select语句更新多行 The answer seems to be for what I want to do. 答案似乎是针对我想做的。

My tables ares as follows. 我的表如下。 tPatientsIDs contains only two columns: keyid and uid. tPatientsID仅包含两列:keyid和uid。

tEyeResults contains a column named patientid and a column named puid. tEyeResults包含一个名为Patientid的列和一个名为puid的列。 The values of patientid match the column uid from tPatientsIDs and puid is empty, having been created by altering the table recently. Patientid的值与tPatientsIDs中的列uid匹配,而puid为空,这是最近通过更改表创建的。

All I want to do is set the value puid to the keyid in tPatientIDs that corresponds to the value of the column patientid. 我要做的就是将值puid设置为tPatientIDs中的keyid,该值与列Patientid的值相对应。

Here is the query I'm using based on the answer on SO: 这是我基于SO答案使用的查询:

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

Even though the structure seems Identical to the answer I get the error: 即使结构似乎与答案完全相同,我仍然收到错误:

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

Use JOIN with your update 在更新中使用JOIN

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

Maybe you need a where clause to only update one patient but I didn't see one in your question 也许您需要一个where子句来仅更新一名患者,但您的问题中我没有看到一个

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM