眾所周知,DATEDIFF() 函數(shù)用于獲取兩個日期之間的天數(shù)差。因此,它也很可能返回負值。
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)
登錄后復(fù)制
以上查詢將返回表“differ”中的值。現(xiàn)在,如果有人想要獲取 OrderDate 和workingDate 之間的差異,那么輸出將為負數(shù),如下所示 –
mysql> Select DATEDIFF(OrderDate, WorkingDate)AS 'DIFFERENCE IN DAYS' from differ; +--------------------+ | DIFFERENCE IN DAYS | +--------------------+ | -7 | | -5 | | -36 | +--------------------+ 3 rows in set (0.00 sec)
登錄后復(fù)制
但是我們可以使用 MySQL ABS() 函數(shù)來忽略這些負值,具體操作如下 –
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)
登錄后復(fù)制
以上就是我們?nèi)绾魏雎?MySQL DATEDIFF() 函數(shù)返回的負值?的詳細內(nèi)容,更多請關(guān)注www.92cms.cn其它相關(guān)文章!