众所周知,DATEDIFF() 函数用于获取两个日期之间的天数差。因此,它也很可能返回负值。
mysql> select * from differ;
+------------+-------------+
| OrderDate | WorkingDate |
+------------+-------------+
| 2017-10-22 | 2017-10-29 |
| 2017-10-25 | 2017-10-30 |
| 2017-10-25 | 2017-11-30 |
+------------+-------------+
3 rows in set (0.00 sec)
登录后复制
以上查询将返回表“differ”中的值。现在,如果有人想要获取 OrderDate 和workingDate 之间的差异,那么输出将为负数,如下所示 -
mysql> Select DATEDIFF(OrderDate, WorkingDate)AS 'DIFFERENCE IN DAYS' from differ;
+--------------------+
| DIFFERENCE IN DAYS |
+--------------------+
| -7 |
| -5 |
| -36 |
+--------------------+
3 rows in set (0.00 sec)
登录后复制
但是我们可以使用 MySQL ABS() 函数来忽略这些负值,具体操作如下 -
mysql> Select ABS(DATEDIFF(OrderDate, WorkingDate))AS 'DIFFERENCE IN DAYS' from differ;
+--------------------+
| DIFFERENCE IN DAYS |
+--------------------+
| 7 |
| 5 |
| 36 |
+--------------------+
3 rows in set (0.00 sec)
登录后复制
以上就是我们如何忽略 MySQL DATEDIFF() 函数返回的负值?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!