首页 > 解决方案 > 如何在 MySql 中按日期顺序显示最近添加的数据?

问题描述

我希望按日期顺序对表格进行排序,以便最近添加的数据位于表格顶部。

我使用查询排序为:

select date from register_table order by date desc.

当前表格显示数据为:

date

02.04.2019
05.04.2019
09.04.2019
10.04.2019
06.02.2019
23.01.2019
11.01.2019

我希望我的表格显示为:

date

10.04.2019
09.04.2019
05.04.2019
02.04.2019
06.02.2019
23.01.2019
11.01.2019

如何按日期顺序显示数据?

标签: mysqlsqldatesql-order-by

解决方案


您的基本问题是不将日期存储为date. 你应该解决这个问题。

要使查询正常工作,请使用:

order by str_to_date(date, '%m.%d.%Y')

要修复数据,您可以执行以下操作:

update register_table
    set date = str_to_date(date, '%m.%d.%Y');

alter table register_table
    modify date date;

你可以在这里看到它是如何工作的。


推荐阅读