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)