简体   繁体   English

SQL,转换时间戳减法的整数

[英]SQL, convert integer for Timestamp subtraction

Assume we have a relation R(A, B), with A contains int values and B contains Timestamps. 假设我们有一个关系R(A,B),A包含int值,B包含Timestamps。

We have to calculate: (time in B in minutes) - (int to minutes) . 我们必须计算:( (time in B in minutes) - (int to minutes)

Example: (125, "2017-06-01 16:23:00") 示例: (125, "2017-06-01 16:23:00")

16:23:00 = 983 min
125 = 125min

983 - 125 = 858min 983-125 = 858分钟

The elements of A represent minutes, my problem is to convert an integer value >59 to hh:mm , since MAKETIME(hh, mm, ss) only works in the range 0 to 59. A的元素代表分钟,我的问题是将整数值>59转换为hh:mm ,因为MAKETIME(hh, mm, ss)仅适用于0到59的范围。

There's no need at all to convert the time of your timestamp column in minutes. 完全没有必要在几分钟内转换时间戳列的时间。

Just do 做就是了

SELECT B - INTERVAL A MINUTE;

If you really just want the time to be subtracted from do 如果你真的只是希望从中减去时间

SELECT TIME(B) - INTERVAL A MINUTE;

To let the date part be untouched: 让日期部分不受影响:

SELECT CONCAT(DATE(B), ' ', TIME(B) - INTERVAL A MINUTE);

When you absolutely need the minutes afterwards: 当你绝对需要以后的会议记录时:

SELECT HOUR(TIME(B) - INTERVAL A MINUTE) * 60 + MINUTE(TIME(B) - INTERVAL A MINUTE);

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

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