【问题标题】:Update column with output of select within the same table in mysql在mysql的同一个表中使用select的输出更新列
【发布时间】:2017-10-19 19:20:26
【问题描述】:

我有一张表,开始和结束时间是这样的

stime | etime

12:00  |  13:20

我现在必须添加带有持续时间的第三列

stime  |  etime  | durationMins

12:00  |  13:20   |  80

我有这个以分钟为单位的持续时间

 SELECT TIME_TO_SEC(TIMEDIFF(stime , etime))/60  AS MinuteDiff 
 FROM mytable

MinuteDiff 现在包含我的分钟数,如何根据上述选择更新所有行的 durationMins?

【问题讨论】:

  • 您通常不会存储派生数据

标签: mysql select sql-update


【解决方案1】:

假设您已经创建了 durationMins 列

 UPDATE Table_name
 set durationMins = TIME_TO_SEC(TIMEDIFF(stime , etime))/60  AS MinuteDiff 

这应该可行。

【讨论】:

  • 工作谢谢,虽然不需要 AS minuteDiff
猜你喜欢
  • 1970-01-01
  • 2014-05-28
  • 1970-01-01
  • 2020-08-14
  • 2021-01-17
  • 1970-01-01
  • 1970-01-01
  • 2012-04-20
  • 2014-12-08
相关资源
最近更新 更多