在 MySQL 查询中添加 45 天间隔的天数,并在新列中显示输出
为此,你可以使用 date_add()。我们先创建一个表 -
mysql> create table DemoTable1930 ( DueTime datetime ); Query OK, 0 rows affected (0.00 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable1930 values('2017-10-21'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1930 values('2019-12-14'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1930 values('2018-11-26'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1930 values('2014-06-16'); Query OK, 1 row affected (0.00 sec)
使用 select 语句显示表中的所有记录 -
mysql> select * from DemoTable1930;
这会产生以下输出 -
+---------------------+ | DueTime | +---------------------+ | 2017-10-21 00:00:00 | | 2019-12-14 00:00:00 | | 2018-11-26 00:00:00 | | 2014-06-16 00:00:00 | +---------------------+ 4 rows in set (0.00 sec)
以下是如何添加天数的查询 -
mysql> select DueTime,date_add(DueTime, interval 45 day) as ManyDays from DemoTable1930;
这会产生以下输出 -
+---------------------+---------------------+ | DueTime | ManyDays | +---------------------+---------------------+ | 2017-10-21 00:00:00 | 2017-12-05 00:00:00 | | 2019-12-14 00:00:00 | 2020-01-28 00:00:00 | | 2018-11-26 00:00:00 | 2019-01-10 00:00:00 | | 2014-06-16 00:00:00 | 2014-07-31 00:00:00 | +---------------------+---------------------+ 4 rows in set (0.00 sec)
广告