我试图进行以下更新:
UPDATE tbl_claim
INNER JOIN tbl_mbr ON tbl_mbr.MBR_NUM = tbl_mbr_cov.MBR_NUM
INNER JOIN tbl_claim ON tbl_claim.MBR_ID = tbl_mbr.MBR_ID
SET tbl_claim.Mbr_Cov_Id = tbl_mbr_cov.MBR_COV_ID
但我得到了:
[Err] 1066 - Not unique table/alias: 'tbl_claim'
然后我尝试了这个:
UPDATE tbl_claim AS A
INNER JOIN tbl_mbr AS C ON C.MBR_NUM = tbl_mbr_cov.MBR_NUM AS D
INNER JOIN tbl_claim AS B ON B.MBR_ID = C.MBR_ID
SET A.Mbr_Cov_Id = D.MBR_COV_ID
这给了我另一个错误:
[Err] 1064 - 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
'as D INNER JOIN tbl_claim AS B ON B.MBR_ID = C.MBR_ID
但我不知道如何为tbl_mbr_cov指定别名?
什么是正确的解决方案?
编辑:
共有3个表格,相关字段为:
tbl_claim:
CLAIM_ID (PK)
MBR_ID
Mbr_Cov_Id
tbl_mbr:
MBR_ID (PK)
MBR_NUM
tbl_mbr_cov:
MBR_COV_ID (PK)
MBR_NUM
如上所示,链接字段名称在3个表中是相同的。 我们想将tbl_claim.Mbr_Cov_Id更新为等于tbl_mbr_cov.MBR_COV_ID
中的值这个查询工作正常,所以我知道连接是有道理的:
SELECT
tbl_claim.CLAIM_ID,
tbl_claim.MBR_ID,
tbl_claim.Mbr_Cov_Id,
tbl_mbr.MBR_ID,
tbl_mbr.MBR_NUM,
tbl_mbr_cov.MBR_COV_ID,
tbl_mbr_cov.MBR_NUM
FROM
tbl_mbr_cov
INNER JOIN tbl_mbr ON tbl_mbr.MBR_NUM = tbl_mbr_cov.MBR_NUM
INNER JOIN tbl_claim ON tbl_claim.MBR_ID = tbl_mbr.MBR_ID
在我看来,如果你可以运行这个查询,必须有一个简单的方法来更新tbl_claim.Mbr_Cov_Id字段。
答案 0 :(得分:0)
试试这句话:
UPDATE tbl_claim
SET Mbr_Cov_Id = tbl_mbr_cov.MBR_COV_ID
FROM tbl_claim AS A
INNER JOIN tbl_mbr_cov ON A.[your_field] = tbl_mbr_cov.[your_field]
INNER JOIN tbl_mbr ON tbl_mbr.MBR_NUM = tbl_mbr_cov.MBR_NUM
INNER JOIN tbl_claim ON tbl_claim.MBR_ID = tbl_mbr.MBR_ID
WHERE [your_primary_key] = A.[your_primary_key]