您可以使用ORDER BY ASC对时间戳值进行升序排列。
以下是不使用TIMESTAMP()
-的语法
SELECT yourTimestampColumnName from yourTableName order by yourTimestampColumnName ASC;
为了理解上述语法,让我们创建一个表。创建表的查询如下-
mysql> create table Timestamp_TableDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> yourTimestamp timestamp -> );
现在,您可以使用insert命令在表中插入一些记录。查询如下-
mysql> insert into Timestamp_TableDemo(yourTimestamp) values('2019-02-06 17:34:57'); mysql> insert into Timestamp_TableDemo(yourTimestamp) values('2019-02-06 17:32:30'); mysql> insert into Timestamp_TableDemo(yourTimestamp) values('2019-02-06 17:32:09');
现在,您可以使用select语句显示表中的所有记录。查询如下-
mysql> select *from Timestamp_TableDemo;
以下是输出-
+----+---------------------+ | Id | yourTimestamp | +----+---------------------+ | 1 | 2019-02-06 17:34:57 | | 2 | 2019-02-06 17:32:30 | | 3 | 2019-02-06 17:32:09 | +----+---------------------+ 3 rows in set (0.00 sec)
这是按时间戳顺序对时间戳值进行排序的查询-
mysql> select yourTimestamp from Timestamp_TableDemo -> order by yourTimestamp ASC;
以下是输出-
+---------------------+ | yourTimestamp | +---------------------+ | 2019-02-06 17:32:09 | | 2019-02-06 17:32:30 | | 2019-02-06 17:34:57 | +---------------------+ 3 rows in set (0.00 sec)