要选择昨天的日期,请使用subdate()
MySQL中的函数。语法如下
select subdate(yourDatetimeColumnName) as anyVariableName from yourTableName;
要了解上述语法,让我们创建一个表
mysql> create table YesterdayDateDemo -> ( -> VisitedDateTime datetime -> );
现在让我们使用insert命令在表中插入日期。查询如下
mysql> insert into YesterdayDateDemo values(now()); mysql> insert into YesterdayDateDemo values('2012-12-26 13:24:35'); mysql> insert into YesterdayDateDemo values('2013-10-22 12:20:32');
现在让我们使用select命令显示表中的所有记录。查询如下
mysql> select *from YesterdayDateDemo;
以下是输出
+---------------------+ | VisitedDateTime | +---------------------+ | 2018-12-18 14:22:48 | | 2012-12-26 13:24:35 | | 2013-10-22 12:20:32 | +---------------------+ 3 rows in set (0.00 sec)
现在,这里是使用subdate()
MySQL的方法为上面插入的日期选择昨天的日期的查询。查询如下
mysql> select subdate(VisitedDateTime,interval 1 day) as YesterdayDate from YesterdayDateDemo;
以下是输出
+---------------------+ | YesterdayDate | +---------------------+ | 2018-12-17 14:22:48 | +---------------------+ 1 row in set (0.00 sec)