眾所周知,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() 函數傳回的負值?的詳細內容。更多資訊請關注PHP中文網其他相關文章!