MySQL日期格式DD/MM/YYYY选择查询?

2021-11-20 00:00:00 mysql

我对如何按日期格式排序有点困惑.

I'm a bit confused on how to order by date formats.

对于 YYYY-MM-DD 格式,您可以这样做:...ORDER BY date DESC...

For the format YYYY-MM-DD you would do this: ...ORDER BY date DESC...

您如何通过DD/MM/YYYY订购?

这不起作用:

SELECT * FROM $table ORDER BY DATE_FORMAT(Date, '%Y%m%d') DESC LIMIT 14

推荐答案

您可以使用 STR_TO_DATE() 将您的字符串转换为 MySQL 日期值和 ORDER BY 结果:

ORDER BY STR_TO_DATE(datestring, '%d/%m/%Y')

但是,明智的做法是将列转换为 DATE 数据类型,而不是使用字符串.

However, you would be wise to convert the column to the DATE data type instead of using strings.

相关文章