要选择时间戳作为MySQL中的日期字符串,语法如下-
select FROM_UNIXTIME(yourColumnName, '%Y-%m-%d %H:%i:%s') from yourTableName;
为了理解上述语法,让我们创建一个表。创建表的查询如下-
mysql> create table select_timestampDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> ArrivalDateTime int -> );
使用insert命令在表中插入一些记录。查询如下-
mysql> insert into select_timestampDemo(ArrivalDateTime) values(1546499730); mysql> insert into select_timestampDemo(ArrivalDateTime) values(1546210820); mysql> insert into select_timestampDemo(ArrivalDateTime) values(1496363157);
现在,您可以使用select语句显示表中的所有记录。查询如下-
mysql> select *from select_timestampDemo;
这是输出-
+----+-----------------+ | Id | ArrivalDateTime | +----+-----------------+ | 1 | 1546499730 | | 2 | 1546210820 | | 3 | 1496363157 | +----+-----------------+ 3 rows in set (0.00 sec)
以下是选择时间戳作为日期字符串的查询-
mysql> select FROM_UNIXTIME(ArrivalDateTime, '%Y-%m-%d %H:%i:%s') from select_timestampDemo;
这是输出-
+-----------------------------------------------------+ | FROM_UNIXTIME(ArrivalDateTime, '%Y-%m-%d %H:%i:%s') | +-----------------------------------------------------+ | 2019-01-03 12:45:30 | | 2018-12-31 04:30:20 | | 2017-06-02 05:55:57 | +-----------------------------------------------------+ 3 rows in set (0.00 sec)