如何在MySQL中从时间戳中选择日期?
要从MySQL的时间戳中选择日期,您需要使用DATE()
。
让我们首先创建一个表-
mysql> create table DemoTable697( Id varchar(100), Title varchar(100), BatchTime timestamp );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable697 values('10','Java','2019-01-21 10:34:56'); mysql> insert into DemoTable697 values('11','Spring','2019-03-11 11:14:16'); mysql> insert into DemoTable697 values('12','Hibernate','2019-07-21 12:04:00');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable697;
这将产生以下输出-
+------+-----------+---------------------+ | Id | Title | BatchTime | +------+-----------+---------------------+ | 10 | Java | 2019-01-21 10:34:56 | | 11 | Spring | 2019-03-11 11:14:16 | | 12 | Hibernate | 2019-07-21 12:04:00 | +------+-----------+---------------------+ 3 rows in set (0.00 sec)
这是从MySQL中的时间戳中选择日期的查询-
mysql> select Title,DATE(BatchTime) AS OnlyDate from DemoTable697 where Id='10';
这将产生以下输出-
+-------+------------+ | Title | OnlyDate | +-------+------------+ | Java | 2019-01-21 | +-------+------------+ 1 row in set (0.00 sec)