MySQL datediff 奇怪的结果
MySQL datediff strange results
datediff 函数的结果很奇怪。
对于不同和相同的时间戳为零?
select datediff('2015-04-25 20:37:45','2015-04-25 05:20:00');
+-------------------------------------------------------+
| datediff('2015-04-25 20:37:45','2015-04-25 05:20:00') |
+-------------------------------------------------------+
| 0 |
+-------------------------------------------------------+
select datediff('2015-04-25 20:37:45','2015-04-25 20:37:45');
+-------------------------------------------------------+
| datediff('2015-04-25 20:37:45','2015-04-25 20:37:45') |
+-------------------------------------------------------+
| 0 |
+-------------------------------------------------------+
如果您查看 MySQL 手册:
DATEDIFF
DATEDIFF() returns expr1 − expr2 expressed as a value in days from one date to the other. expr1 and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation
所以你得到的是正确的。
如果您正在寻找时差,我建议您查看 TIMEDIFF
or TIMESTAMPDIFF
。
如果您想得到分钟数的差异,最好的方法是TIMESTAMPDIFF
。
这里有一个你如何做的例子:
SELECT TIMESTAMPDIFF(MINUTE, '2015-04-25 20:37:45','2015-04-25 05:20:00');
+--------------------------------------------------------------------+
| TIMESTAMPDIFF(MINUTE, '2015-04-25 20:37:45','2015-04-25 05:20:00') |
+--------------------------------------------------------------------+
| -917 |
+--------------------------------------------------------------------+
datediff
将对日期进行计算并找出天数差异,
但是您可以使用 timestampdiff 来表示不同的差异,例如 hour
、min
等
mysql> select timestampdiff(hour,'2015-04-25 05:20:00','2015-04-25 20:37:45') as hour;
+------+
| hour |
+------+
| 15 |
+------+
1 row in set (0.00 sec)
如果你想比较两个时间戳的差异小于一天,你应该使用 TIMEDIFF() 函数而不是 DATEDIFF。例如:
select TIMEDIFF('2015-04-25 20:37:45','2015-04-25 05:20:00');
或秒内的结果:
select TIME_TO_SEC(TIMEDIFF('2015-04-25 20:37:45','2015-04-25 05:20:00'));
更多详情:
MySQL: how to get the difference between two timestamps in seconds
希望这对您有所帮助:)
datediff 函数的结果很奇怪。 对于不同和相同的时间戳为零?
select datediff('2015-04-25 20:37:45','2015-04-25 05:20:00');
+-------------------------------------------------------+
| datediff('2015-04-25 20:37:45','2015-04-25 05:20:00') |
+-------------------------------------------------------+
| 0 |
+-------------------------------------------------------+
select datediff('2015-04-25 20:37:45','2015-04-25 20:37:45');
+-------------------------------------------------------+
| datediff('2015-04-25 20:37:45','2015-04-25 20:37:45') |
+-------------------------------------------------------+
| 0 |
+-------------------------------------------------------+
如果您查看 MySQL 手册:
DATEDIFF
DATEDIFF() returns expr1 − expr2 expressed as a value in days from one date to the other. expr1 and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation
所以你得到的是正确的。
如果您正在寻找时差,我建议您查看 TIMEDIFF
or TIMESTAMPDIFF
。
如果您想得到分钟数的差异,最好的方法是TIMESTAMPDIFF
。
这里有一个你如何做的例子:
SELECT TIMESTAMPDIFF(MINUTE, '2015-04-25 20:37:45','2015-04-25 05:20:00');
+--------------------------------------------------------------------+
| TIMESTAMPDIFF(MINUTE, '2015-04-25 20:37:45','2015-04-25 05:20:00') |
+--------------------------------------------------------------------+
| -917 |
+--------------------------------------------------------------------+
datediff
将对日期进行计算并找出天数差异,
但是您可以使用 timestampdiff 来表示不同的差异,例如 hour
、min
等
mysql> select timestampdiff(hour,'2015-04-25 05:20:00','2015-04-25 20:37:45') as hour;
+------+
| hour |
+------+
| 15 |
+------+
1 row in set (0.00 sec)
如果你想比较两个时间戳的差异小于一天,你应该使用 TIMEDIFF() 函数而不是 DATEDIFF。例如:
select TIMEDIFF('2015-04-25 20:37:45','2015-04-25 05:20:00');
或秒内的结果:
select TIME_TO_SEC(TIMEDIFF('2015-04-25 20:37:45','2015-04-25 05:20:00'));
更多详情:
MySQL: how to get the difference between two timestamps in seconds
希望这对您有所帮助:)