MySQL无法在FROM多个表连接中为更新指定目标表

时间:2016-03-17 03:38:17

标签: mysql join sql-update

在尝试从多个连接表更新字段时,我已经搜索了几天如何解决此错误,并且来自同一多个连接表集的最小日期。

这是我的更新声明:

update vtiger_projectmilestone 
Inner Join vtiger_projectmilestonecf  ON vtiger_projectmilestone.projectmilestoneid = vtiger_projectmilestonecf.projectmilestoneid
Inner Join vtiger_crmentity ON vtiger_projectmilestone.projectmilestoneid = vtcrmm.crmid
inner join vtiger_project on vtiger_project.projectid = vtiger_projectmilestone.projectid
Inner Join vtiger_crmentity vtcrmp ON vtcrmp.crmid = vtiger_project.projectid

 set vtiger_projectmilestone.projectmilestonedate =
(select min(vtiger_projecttaskcf.cf_779) 
FROM vtiger_projecttask tvpt
Inner Join vtiger_projecttaskcf tvptcf ON tvpt.projecttaskid  = tvptcf.projecttaskid
Inner Join vtiger_projectmilestone tvpm ON tvpm.projectmilestoneid = tvpt.projecttasknumber
Inner Join vtiger_projectmilestonecf vtpmcf ON tvpm.projectmilestoneid = tvpmcf.projectmilestoneid
Inner Join vtiger_crmentity AS vtcrmm ON tvpm.projectmilestoneid = vtcrmm.crmid
Inner Join vtiger_crmentity AS vtcrmt ON tvpt.projecttaskid = vtcrmt.crmid
where tvpm.projectmilestone_no = vtiger_projectmilestone.projectmilestone_no
) 
where vtiger_projectmilestone.projectid = 
(select vtiger_project.projectid from vtiger_project 
INNER JOIN vtiger_crmentity vtcrmp ON vtiger_project.projectid = vtcrmp.crmid
where vtcrmp.deleted = 0 order by vtiger_project.projectid desc limit 1)
and vtcrmp.deleted = 0
and vtcrmm.deleted = 0 
and (vtiger_projectmilestone.projectmilestonedate is null or      vtiger_projectmilestonecf.cf_763 is null) ;

这是一个真实的更新查询,而不仅仅是一个简单的表关系。

我通过创建临时表,插入值,更新目标表和删除临时表来解决这个问题。

我真的很想做到这一点,因为它会更频繁地出现。

感谢所有的帮助。

干杯 伯纳德贝利

1 个答案:

答案 0 :(得分:0)

this answer中所述,您无法在子查询中使用目标更新表,正如您在查询中看到的那样

SELECT min(vtiger_projecttaskcf.cf_779) 
FROM vtiger_projecttask tvpt
Inner Join vtiger_projecttaskcf tvptcf ON tvpt.projecttaskid  = tvptcf.projecttaskid
Inner Join 
--using target update table in query
vtiger_projectmilestone tvpm ON tvpm.projectmilestoneid = tvpt.projecttasknumber 
Inner Join vtiger_projectmilestonecf vtpmcf ON tvpm.projectmilestoneid = tvpmcf.projectmilestoneid
Inner Join vtiger_crmentity AS vtcrmm ON tvpm.projectmilestoneid = vtcrmm.crmid
Inner Join vtiger_crmentity AS vtcrmt ON tvpt.projecttaskid = vtcrmt.crmid
where tvpm.projectmilestone_no = vtiger_projectmilestone.projectmilestone_no

但是,我认为解决方法是使用您正在更新的表中的数据,因此您可以在某些条件中编写一些例如:

而不是使用连接。
SELECT min(vtiger_projecttaskcf.cf_779) 
FROM vtiger_projecttask tvpt
Inner Join vtiger_projecttaskcf tvptcf ON tvpt.projecttaskid  = tvptcf.projecttaskid 
Inner Join vtiger_projectmilestonecf vtpmcf ON tvpm.projectmilestoneid = tvpt.projecttasknumber
Inner Join vtiger_crmentity AS vtcrmm ON tvpt.projecttasknumber = vtcrmm.crmid
Inner Join vtiger_crmentity AS vtcrmt ON tvpt.projecttaskid = vtcrmt.crmid
where tvpm.projectmilestone_no = vtiger_projectmilestone.projectmilestone_no
--using the projectmilestoneid in a where clause
AND tvpt.projecttasknumber=vtiger_projectmilestone.projectmilestoneid 

警告可能是你可能会遇到一些性能问题,因为我不知道完整的架构,我无法判断在子查询中使用其他表而不是vtiger_projectmilestone会给你正确的结果