开发者

correlated subquery update

开发者 https://www.devze.com 2022-12-09 05:54 出处:网络
I have decided in my table that I no longer want to record start and finish times, but rather just a start time and a duration in minutes.How can I update my table so that my new column has its values

I have decided in my table that I no longer want to record start and finish times, but rather just a start time and a duration in minutes. How can I update my table so that my new column has its values inserted based on the existing data? my开发者_高级运维 attempt below yields the error:

You can't specify target table 'lesson' for update in FROM clause

UPDATE lesson 
SET duration = 
    (SELECT TIME_TO_SEC(TIMEDIFF(finish_time,start_time))/60 
     FROM lesson AS l 
     WHERE l.id = lesson.id)


You dont have to do that because your updating a column with values of other columns in the same row, do just:

UPDATE lesson 
SET duration = TIME_TO_SEC(TIMEDIFF(finish_time,start_time))/60 
0

精彩评论

暂无评论...
验证码 换一张
取 消