MySQLdatediff()函数
转载⾃
MySQL DATEDIFF函数介绍
MySQL DATEDIFF函数计算两个,或值之间的天数。
MySQL DATEDIFF函数的语法如下:
DATEDIFF(date_expression_1,date_expression_2);
DATEDIFF函数接受两个任何有效⽇期或⽇期时间值的参数。如果您传递DATETIME或TIMESTAMP值,则DATEDIFF函数仅将⽇期部分⽤于计算,并忽略时间部分。
DATEDIFF函数在许多情况下很有⽤,例如,您可以计算产品需要发送给客户的时间。
MySQL DATEDIFF⽰例
我们来看⼀下使⽤DATEDIFF函数的⼀些例⼦。
⽰例-1
mysql> SELECT DATEDIFF('2017-08-17','2017-08-17');
+-------------------------------------+
| DATEDIFF('2017-08-17','2017-08-17') |
+-------------------------------------+
| 0 |
+-------------------------------------+
1 row in set
⽰例-2
mysql> SELECT DATEDIFF('2017-08-17','2017-08-08');
+-------------------------------------+
| DATEDIFF('2017-08-17','2017-08-08') |
+-------------------------------------+
| 9 |
+-------------------------------------+
1 row in set
⽰例-3
mysql> SELECT DATEDIFF('2017-08-08','2017-08-17');
+-------------------------------------+
| DATEDIFF('2017-08-08','2017-08-17') |
+-------------------------------------+
| -9 |
+-------------------------------------+
1 row in set
请参阅中的以下orders表,其结构如下 -
mysql> desc orders;
+----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| orderNumber | int(11) | NO | PRI | NULL | |
| orderDate | date | NO | | NULL | |
| requiredDate | date | NO | | NULL | |
| shippedDate | date | YES | | NULL | |
| status | varchar(15) | NO | | NULL | |
| comments | text | YES | | NULL | |
| customerNumber | int(11) | NO | MUL | NULL | |
+----------------+-------------+------+-----+---------+-------+
7 rows in set
假设使⽤DATEDIFF函数计算所需⽇期和订单发货⽇期之间的天数,如下所⽰:
SELECT
orderNumber,
DATEDIFF(requiredDate, shippedDate) daysLeft
FROM
orders
ORDER BY daysLeft DESC;
执⾏上⾯查询语句,得到以下结果 -
+-------------+----------+
| orderNumber | daysLeft |
+-------------+----------+
| 10409 | 11 |
| 10410 | 10 |
| 10105 | 9 |
| 10135 | 9 |
| 10190 | 9 |
**** 此处省略了⼀⼤⼩波数据 ********
| 10423 | NULL |
| 10424 | NULL |
| 10425 | NULL |
+-------------+----------+
327 rows in set
以下语句将获取状态正在处理(In Process)的所有订单,并计算订单⽇期和所需⽇期之间的天数:
SELECT
orderNumber,
DATEDIFF(requiredDate, orderDate) remaining_days
FROM
datediff是字符型函数orders
WHERE
status = 'In Process'
ORDER BY remaining_days;
执⾏上⾯查询语句,得到以下结果 -
+-------------+----------------+
| orderNumber | remaining_days |
+-------------+----------------+
| 10426 | 5 |
| 10423 | 6 |
| 10425 | 7 |
| 10421 | 8 |
| 10424 | 8 |
| 10420 | 9 |
| 10422 | 12 |
+-------------+----------------+
7 rows in set
要计算周或⽉作为间隔时间,可以将DATEDIFF函数的返回值除以7或30,如下查询语句:
SELECT
orderNumber,
ROUND(DATEDIFF(requiredDate, orderDate) / 7, 2) as weeks,
ROUND(DATEDIFF(requiredDate, orderDate) / 30,2) as months
FROM
orders
WHERE
status = 'In Process';
执⾏上⾯查询语句,得到以下结果 -
+-------------+-------+--------+
| orderNumber | weeks | months |
+-------------+-------+--------+
| 10420 | 1.29 | 0.3 |
| 10421 | 1.14 | 0.27 |
| 10422 | 1.71 | 0.4 |
| 10423 | 0.86 | 0.2 |
| 10424 | 1.14 | 0.27 |
| 10425 | 1 | 0.23 |
| 10426 | 0.71 | 0.17 |
+-------------+-------+--------+
7 rows in set
请注意,ROUND函数⽤于舍⼊结果。
在本教程中,您已经学习了如何使⽤MySQL DATEDIFF函数来计算两个⽇期值之间的天数。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论