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

时间:2017-07-24 10:46:29

标签: mysql subquery mysql-error-1064

我在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”的dataStartDate到处出现F2EAACC7-607F-E611-91D1-34028608A344)... 做这个查询给了我最早的价值并且效果很好........

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

当我尝试通过subselect更新时出现问题..我认为这样可行....

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;

给出错误....

  

错误1064(42000):您的SQL语法有错误;检查   手册,对应右边的MySQL服务器版本   在'FROM(SELECT ProjectUID,min(dataStartDate)附近使用的语法   dataStartDate FROM PPMload GROUP BY'在第1行

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

1 个答案:

答案 0 :(得分:2)

您需要使用像

这样的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