本文介紹了如何在 MySQL 上獲取兩個日期之間相差的天數?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我需要獲取 MySQL 上幾個日期中包含的天數.
I need to get the number of days contained within a couple of dates on MySQL.
例如:
- 入住日期是
12-04-2010
- 退房日期
15-04-2010
日差為 3.
推薦答案
DATEDIFF 函數?
What about the DATEDIFF function ?
引用手冊頁:
DATEDIFF() 返回 expr1 – expr2表示為從一開始的天數約會到另一個.expr1 和 expr2是日期或日期和時間表達式.只有值的日期部分是用于計算
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
在你的情況下,你會使用:
In your case, you'd use :
mysql> select datediff('2010-04-15', '2010-04-12');
+--------------------------------------+
| datediff('2010-04-15', '2010-04-12') |
+--------------------------------------+
| 3 |
+--------------------------------------+
1 row in set (0,00 sec)
但請注意,日期應寫為YYYY-MM-DD
,而不是像您發布的那樣DD-MM-YYYY
.
But note the dates should be written as YYYY-MM-DD
, and not DD-MM-YYYY
like you posted.
這篇關于如何在 MySQL 上獲取兩個日期之間相差的天數?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!