我有以下 MySQL 表结构:
PositionDateTime [DateTime]
Hour [Int]
我想添加一个名为 BaseDateTime 的附加列,它将通过采用 PositionDateTime - 小时来计算
Example of desired result:
PositionDateTime: 2015-07-02 12:00:00
Hour: 6
BaseDateTime: 2015-07-02 06:00:00
感谢您的任何建议或sql语句。 问候, 布莱恩
最佳答案
Use the MYSQL function
DATEADD(datepart,number,date)
where,
date
is a valid date expression,number
is the number of interval units you want to add or subtract &datepart
can behh
for hours,d
for days,ss
for seconds, etc.
所以,在你的情况下,它变成:
SELECT DATEADD(hh,Hour,PositionDateTime) AS BaseDateTime FROM <TABLE_NAME>;
有关此功能的更多信息,请访问:this W3Schools link
关于Mysql 添加根据其他 2 列计算的日期时间列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31664939/