如何用select查询在mysql中编写update语句?

时间:2013-12-14 12:26:49

标签: mysql

我希望仅在max(date) and min(Date) from Table2table1.status=100table1.Todate='0000-00-00 00:00:00'更新table1 同样的事情我正在尝试跟随查询,但它在组中给出错误

update table1 s 
left join table2  t 
on s.stCode=t.tsTask 
set s.stActFrom= min(t.tsDate),s.stActTo=max(t.tsDate)
WHERE s.stActTo='0000-00-00 00:00:00' and s.stStatus=100 
group by t.`tsTask`

如果我执行此查询,则会出现以下错误。 #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 'group by t. {tsTask {1}}

任何人都可以告诉我如何编写此更新查询吗?

2 个答案:

答案 0 :(得分:0)

update s
set s.stActFrom= t.MinDate,
    s.stActTo  = t.MaxDate
FROM table1 s left join 
                       (
                        SELECT MinDate = min(tsDate), MaxDate = max(tsDate)
                        FROM table2   
                        group by `tsTask`
                       ) AS t
WHERE s.stActTo='0000-00-00 00:00:00' and s.stStatus=100 
ON s.stCode=t.tsTask 

答案 1 :(得分:0)

我已经尝试了上述答案。这没有给我所需的答案。所以我从以下查询中得到了答案。

  update table1 s left join 
                   (
                    SELECT min( tsDate ) AS MinDates, max( tsDate ) AS MaxDates, tsTask
                    FROM table2   
                    group by `tsTask`
                   ) AS t ON s.stCode=t.tsTask 
  set s.stActFrom= t.MinDate,
  s.stActTo  = t.MaxDate      
  WHERE s.stActTo='0000-00-00 00:00:00' and s.stStatus=100