使用UPDATE,INNER JOIN,MAX和GROUP BY

时间:2016-09-19 23:32:35

标签: mysql group-by inner-join

我有一个表格课程,我想从其他2个表格,场地和课程中复制数据。

课程可以链接到1个场地,可以有很多课程。

我想根据上一课日期设定课程结束日期。

目前我有以下代码但它不适用于我的GROUP BY语句。

UPDATE course AS c
INNER JOIN venue as v.id = c.venue_id
INNER JOIN lesson l ON c.id = l.course_id
   SET
     c.venue_name = v.name,
     c.end_date = MAX(l.date)
WHERE c.status IS NULL
GROUP BY l.course_id

以下是一种合适且有效的方法吗?

UPDATE course AS c
INNER JOIN venue as v.id = c.venue_id
   SET
     c.venue_name = v.name,
     c.end_date = (SELECT MAX(l.date) FROM lesson INNER JOIN lesson l ON c.id = l.course_id GROUP BY l.course_id)
WHERE c.status IS NULL

0 个答案:

没有答案