MySQL查询从时间戳中选择日期?
让我们首先创建一个表。列之一是时间戳-
mysql> create table DemoTable -> ( -> CustomerName varchar(100), -> CustomerShippingDate timestamp -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('Chris','2019-01-21'); mysql> insert into DemoTable values('David','2019-03-01'); mysql> insert into DemoTable values('Robert','2019-06-04');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
输出结果
这将产生以下输出-
+--------------+----------------------+ | CustomerName | CustomerShippingDate | +--------------+----------------------+ | Chris | 2019-01-21 00:00:00 | | David | 2019-03-01 00:00:00 | | Robert | 2019-06-04 00:00:00 | +--------------+----------------------+ 3 rows in set (0.00 sec)
以下是查询以选择时间戳记的日期-
mysql> select *,date(CustomerShippingDate) from DemoTable;
输出结果
这将产生以下输出-
+--------------+----------------------+----------------------------+ | CustomerName | CustomerShippingDate | date(CustomerShippingDate) | +--------------+----------------------+----------------------------+ | Chris | 2019-01-21 00:00:00 | 2019-01-21 | | David | 2019-03-01 00:00:00 | 2019-03-01 | | Robert | 2019-06-04 00:00:00 | 2019-06-04 | +--------------+----------------------+----------------------------+ 3 rows in set (0.00 sec)