MySQL DATE 函数返回当前日期和加入日期之间的差值
mysqlmysqli database
首先,找到当前日期,并使用 DATEDIFF() 获取加入日期和当前日期之间的差值。
当前日期如下 −
mysql> select curdate(); +------------+ | curdate() | +------------+ | 2019-10-26 | +------------+ 1 row in set (0.00 sec)
首先我们创建一个表 −
mysql> create table DemoTable -> ( -> JoiningDate varchar(40) -> ); Query OK, 0 rows affected (0.61 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values('10/10/1998'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('31/12/2010'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('01/01/2017'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('25/10/2019'); Query OK, 1 row affected (0.14 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
这将产生以下输出 −
+-------------+ | JoiningDate | +-------------+ | 10/10/1998 | | 31/12/2010 | | 01/01/2017 | | 25/10/2019 | +-------------+ 4 rows in set (0.00 sec)
以下查询返回当前日期和加入日期之间的差值 −
mysql> select datediff(curdate(),str_to_date(JoiningDate,'%d/%m/%Y')) from DemoTable;
这将产生以下输出 −
+---------------------------------------------------------+ | datediff(curdate(),str_to_date(JoiningDate,'%d/%m/%Y')) | +---------------------------------------------------------+ | 7686 | | 3221 | | 1028 | | 1 | +---------------------------------------------------------+ 4 rows in set (0.00 sec)