要比较MySQL中的时间戳,可以使用DATE()
。让我们首先创建一个表-
mysql> create table comparingTimestampDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> AdmissionDate timestamp -> );
以下是使用insert命令在表中插入记录的查询-
mysql> insert into comparingTimestampDemo(AdmissionDate) values('2019-03-31'); mysql> insert into comparingTimestampDemo(AdmissionDate) values('2019-04-10'); mysql> insert into comparingTimestampDemo(AdmissionDate) values('2019-04-15'); mysql> insert into comparingTimestampDemo(AdmissionDate) values('2019-03-29'); mysql> insert into comparingTimestampDemo(AdmissionDate) values('2019-04-07');
以下是使用select语句显示表中所有记录的查询-
mysql> select * from comparingTimestampDemo;
这将产生以下输出-
+----+---------------------+ | Id | AdmissionDate | +----+---------------------+ | 1 | 2019-03-31 00:00:00 | | 2 | 2019-04-10 00:00:00 | | 3 | 2019-04-15 00:00:00 | | 4 | 2019-03-29 00:00:00 | | 5 | 2019-04-07 00:00:00 | +----+---------------------+ 5 rows in set (0.00 sec)
现在让我们比较MySQL中的时间戳-
mysql> SELECT DATE(`AdmissionDate`) FROM comparingTimestampDemo WHERE DATE(`AdmissionDate`) < CURDATE() - INTERVAL 3 DAY;
这将产生以下输出-
+-----------------------+ | DATE(`AdmissionDate`) | +-----------------------+ | 2019-03-31 | | 2019-03-29 | +-----------------------+ 2 rows in set (0.00 sec)