让我们首先创建一个表-
mysql> create table DemoTable -> ( -> Date1 date, -> Date2 date -> );
使用插入命令&miuns;在表中插入一些记录。
mysql> insert into DemoTable values('2017-01-10','2017-12-10'); mysql> insert into DemoTable values('2018-12-31','2015-01-02'); mysql> insert into DemoTable values('2020-03-01','2019-06-15');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+------------+------------+ | Date1 | Date2 | +------------+------------+ | 2017-01-10 | 2017-12-10 | | 2018-12-31 | 2015-01-02 | | 2020-03-01 | 2019-06-15 | +------------+------------+ 3 rows in set (0.00 sec)
这是查询以月的形式查找日期之间的差异-
mysql> select abs(timestampdiff(MONTH,Date1,Date2)) from DemoTable;
这将产生以下输出-
+---------------------------------------+ | abs(timestampdiff(MONTH,Date1,Date2)) | +---------------------------------------+ | 11 | | 47 | | 8 | +---------------------------------------+ 3 rows in set (0.03 sec)