mysql 计算两个日期之间的天数
需求:求现在与指定日期之间相隔天数
首先创建一个包含时间字段的表格T如下:
mysql> select * from T;
+-------------+
| search_time |
+-------------+
| 2019-8-10 |
| 2018-8-10 |
| 2019-9-10 |
+-------------+
3 rows in set (0.00 sec)
使用 diff_format 函数检索现在的时间:
mysql> select date_format(now(),"%Y-%m-%d") ;
+-------------------------------+
| date_format(now(),"%Y-%m-%d") |
+-------------------------------+
| 2020-08-20 |
+-------------------------------+
1 row in set (0.00 sec)
使用 datediff 函数求两个日期之间的天数:
mysql> select datediff(date_format(now(),"%Y-%m-%d"),search_time) from T;
+-----------------------------------------------------+
| datediff(date_format(now(),"%Y-%m-%d"),search_time) |
+-----------------------------------------------------+
| 376 |
| 741 |
| 345 |
+-----------------------------------------------------+
3 rows in set (0.00 sec)