如何在MySQL中插入带有时间戳“ X days ago”的行?
要插入这样的行,请使用以下语法,其中我们使用CURRENT_TIMESTAMP-
insert into yourTableName values(CURRENT_TIMESTAMP - INTERVAL ABS(RAND() * 100) DAY);
要了解上述语法,让我们创建一个表-
mysql> create table DemoTable1983 ( DueDate timestamp );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1983 values(CURRENT_TIMESTAMP - INTERVAL ABS(RAND() * 100) DAY); mysql> insert into DemoTable1983 values(CURRENT_TIMESTAMP - INTERVAL ABS(RAND() * 100) DAY); mysql> insert into DemoTable1983 values(CURRENT_TIMESTAMP - INTERVAL ABS(RAND() * 100) DAY);
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1983;
这将产生以下输出-
+---------------------+ | DueDate | +---------------------+ | 2019-12-10 19:35:43 | | 2019-10-27 19:35:44 | | 2019-11-10 19:35:45 | +---------------------+ 3 rows in set (0.00 sec)
当前日期时间如下-
mysql> select now();
这将产生以下输出-
+---------------------+ | now() | +---------------------+ | 2019-12-18 19:36:17 | +---------------------+ 1 row in set (0.00 sec)