mysql中实现按时间戳排序的方法
1.首先,首先创建一个数据表;
create table DemoTable
(
`timestamp` varchar(100)
);
2.数据表创建好后,向表中插入时间戳数据;
insert into DemoTable values('06/22/2019 01:10:20 PM');
insert into DemoTable values('06/22/2019 12:00:27 PM');
insert into DemoTable values('06/22/2019 06:56:20 AM');
insert into DemoTable values('06/22/2019 07:10:11 AM');
3.时间戳数据插入后,查看表结构;
select *from DemoTable;
+------------------------+
| timestamp |
+------------------------+
| 06/22/2019 01:10:20 PM |
| 06/22/2019 12:00:27 PM |
| 06/22/2019 06:56:20 AM |
| 06/22/2019 07:10:11 AM |
+------------------------+
4 rows in set (0.00 sec)
4.通过履行以下命令对表中的时间戳进行排序;
select *from DemoTable ORDER BY STR_TO_DATE(`timestamp`,'%m/%d/%Y%h:%i:%s %p');
5.最后,时间戳排序好后,继续查看表结构,便可看到时间戳排序成功;
select *from DemoTable;
+------------------------+
| timestamp |
+------------------------+
| 06/22/2019 06:56:20 AM |
| 06/22/2019 07:10:11 AM |
| 06/22/2019 12:00:27 PM |
| 06/22/2019 01:10:20 PM |
+------------------------+
4 rows in set (0.00 sec)
本文来源:https://www.yuntue.com/post/58298.html | 云服务器网,转载请注明出处!