我有3个文件id
,startdate
,enddate
的表格。现在我添加了一个名为mins
的新字段,如图中所示。
我需要查询才能在几分钟内获得startdate
和enddate
之间的差异,并存储在mysql字段中。
答案 0 :(得分:0)
使用MySQL TIMESTAMPDIFF()
:
UPDATE YourTable t
set t.mins = ABS(TIMESTAMPDIFF(MINUTE,t.startdate,t.enddate)) ;
答案 1 :(得分:0)
如果您稍后添加了分钟列,则应使用以下查询更新表。
INSERT INTO yourTable
SELECT
inn.id,
inn.StartDate,
inn.EndDate,
TIMESTAMPDIFF(
MINUTE,
inn.StartDate,
inn.EndDate
) AS diff
FROM
`yourTable` inn
ON DUPLICATE KEY
UPDATE
id = inn.id,
`StartDate` = inn.StartDate,
EndDate = inn.EndDate,
Mins = TIMESTAMPDIFF(
MINUTE,
inn.StartDate,
inn.EndDate
) ;
假设这是表格的初始状态
id StartDate EndDate Mins
------ ------------------- ------------------- --------
1 2016-11-07 12:53:27 2016-11-30 12:53:35 0
2 2016-11-02 12:53:41 2016-12-07 12:53:45 0
3 2016-11-16 12:53:52 2017-02-16 12:53:56 0
执行上述查询会转换表格如下:
id StartDate EndDate Mins
------ ------------------- ------------------- --------
1 2016-11-07 12:53:27 2016-11-30 12:53:35 33120
2 2016-11-02 12:53:41 2016-12-07 12:53:45 50400
3 2016-11-16 12:53:52 2017-02-16 12:53:56 132480