mysql如何时间戳排序

1290
2020/12/17 12:15:07
栏目: 云计算
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

mysql如何时间戳排序

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)

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: mysql数据库连接不上怎么解决