简体   繁体   English

如何使用选择查询在mysql中编写更新语句?

[英]How Can i write update statement in mysql with select query?

I want to update table1 by taking max(date) and min(Date) from Table2 for only if table1.status=100 and table1.Todate='0000-00-00 00:00:00' same thing I am trying with following query but it is giving error in group by 我想仅通过table1.status=100table1.Todate='0000-00-00 00:00:00' max(date) and min(Date) from Table2获取max(date) and min(Date) from Table2来更新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`

if i execute this query i am getting following error. 如果我执行此查询,我得到以下错误。 #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 ' at line 1 tsTask ' at line 1

Can any one tell me how i can write this update query? 谁能告诉我如何编写此更新查询?

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 

I have tried the above answers. 我已经尝试了以上答案。 That is not given me the required answer. 这没有给我所需的答案。 So i have got answer from the following query. 所以我从以下查询中得到了答案。

  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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM