使用子选择更新 MySQL -- MIN(date)

Updating MySQL with a subselect -- MIN(date)

我在 MySQl 中有这样的数据...

|         ProjectUID                   |    dataStartDate    |
| F2EAACC7-607F-E611-91D1-34028608A344 | 2017-04-14 10:00:00 |
| F2EAACC7-607F-E611-91D1-34028608A344 | 2017-02-23 16:00:00 |
| F2EAACC7-607F-E611-91D1-34028608A344 | 2017-02-24 08:00:00 |
| F2EAACC7-607F-E611-91D1-34028608A344 | 2017-02-24 15:00:00 |
| F2EAACC7-607F-E611-91D1-34028608A344 | 2017-02-24 23:30:00 |
| F2EAACC7-607F-E611-91D1-34028608A344 | 2017-05-24 08:00:00 |
| F2EAACC7-607F-E611-91D1-34028608A344 | 2017-11-06 08:00:00 |
| F2EAACC7-607F-E611-91D1-34028608A344 | 2017-05-23 08:00:00 |
| F2EAACC7-607F-E611-91D1-34028608A344 | 2017-11-03 08:00:00 |
| F2EAACC7-607F-E611-91D1-34028608A344 | 2017-05-23 08:00:00 |
| F2EAACC7-607F-E611-91D1-34028608A344 | 2017-11-03 08:00:00 |

我需要将 dataStartDate 更新为唯一 ProjectUID 的最早值(在这种情况下,将 ProjectUID:F2EAACC7-607F-E611-91D1-34028608A344 更改为“2017-02-23 16:00:00”到处都出现 F2EAACC7-607F-E611-91D1-34028608A344)... 执行此查询为我提供了最早的价值并且效果很好........

SELECT ProjectUID, min(dataStartDate) dataStartDate 
FROM PPMload_temp 
GROUP BY ProjectUID;

当我尝试通过子选择更新时出现问题。我认为这会起作用....

UPDATE PPMload_temp c 
SET c.dataStartDate=x.dataStartDate 
FROM 
(SELECT ProjectUID, min(dataStartDate) dataStartDate FROM PPMload 
GROUP BY ProjectUID) x where c.ProjectUID = x.ProjectUID;

这给出了错误....

ERROR 1064 (42000): 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 (SELECT ProjectUID, min(dataStartDate) dataStartDate FROM PPMload GROUP BY ' at line 1

关于我哪里出错的任何想法?非常感谢! JW

您需要使用 update-join 结构,例如

UPDATE PPMload_temp c 
JOIN 
(SELECT ProjectUID, min(dataStartDate) dataStartDate FROM PPMload 
GROUP BY ProjectUID) x ON c.ProjectUID = x.ProjectUID
SET c.dataStartDate = x.dataStartDate