MySQL 查询计算两行不同列但相似的行之间的天数
让我们首先创建一个表 -
mysql> create table DemoTable1471 -> ( -> EmployeeJoiningDate date, -> EmployeeRelievingDate date -> ); Query OK, 0 rows affected (0.57 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable1471 values('2018-06-21','2018-12-21'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1471 values('2017-01-19','2019-01-31'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1471 values('2015-12-31','2016-03-01'); Query OK, 1 row affected (0.11 sec)
使用 select 语句显示表中的所有记录 -
mysql> select * from DemoTable1471;
这将产生以下输出 -
+---------------------+-----------------------+ | EmployeeJoiningDate | EmployeeRelievingDate | +---------------------+-----------------------+ | 2018-06-21 | 2018-12-21 | | 2017-01-19 | 2019-01-31 | | 2015-12-31 | 2016-03-01 | +---------------------+-----------------------+ 3 rows in set (0.00 sec)
这是计算两个日期之间天数的查询 -
mysql> select datediff(EmployeeRelievingDate,EmployeeJoiningDate) as EmployeeWorkDays from DemoTable1471;
这将产生以下输出 -
+------------------+ | EmployeeWorkDays | +------------------+ | 183 | | 742 | | 61 | +------------------+ 3 rows in set (0.00 sec)
广告