从时间戳中选择日期的 MySQL 查询?
我们首先创建一个表。其中一列是一个时间戳 -
mysql> create table DemoTable -> ( -> CustomerName varchar(100), -> CustomerShippingDate timestamp -> ); Query OK, 0 rows affected (0.52 sec)
使用 insert 命令插入一些记录到表中 -
mysql> insert into DemoTable values('Chris','2019-01-21'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('David','2019-03-01'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('Robert','2019-06-04'); Query OK, 1 row affected (0.25 sec)
使用 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)
广告