简体   繁体   English

如何使用mysql使用月份和日期进行排序

[英]How to sort using month and day using mysql

I have a date in this format. 我有这种格式的日期。

08 april 1989
02 December 1984
13 January 1986 

I would like to sort the results using month and day ,which has column "dateofb" and i sorting is like 我想使用的月份和日期对结果进行排序,其中有列“dateofb”我选像

13 january 1986
08 april 1989
02 december 1984

I have used the below code which doesn't work fine , 我使用了下面的代码,它不能正常工作,

$sel = $db->query("select * from biography where dateofb >= (CURDATE() - INTERVAL 90 DAY) order by dateofb desc limit 0,3");

I would like to display the 3 sorted results coming 90days. 我想显示90天后的3个排序结果。

Is it possible to convert the dateofb column to DATE column type? 是否可以将dateofb列转换为DATE列类型? This would allow you to do what you're looking for. 这将使您可以执行所需的操作。 The format you have now is invalid and would need to be converted via STR_TO_DATE(dateofb, '%d %M %Y') 您现在使用的格式无效,需要通过STR_TO_DATE(dateofb, '%d %M %Y')

Example: 例:

$sel = $db->query("select * from biography where STR_TO_DATE(dateofb, '%d %M %Y') >= (CURDATE() - INTERVAL 90 DAY) order by STR_TO_DATE(dateofb, '%d %M %Y') desc limit 0,3");

^- See how gross that looks? ^-看看外观如何? If your dateofb was a DATE column, you could just do: 如果您的dateofbDATE列,则可以执行以下操作:

$sel = $db->query("select * from biography where dateofb >= (CURDATE() - INTERVAL 90 DAY) order by dateofb desc limit 0,3");

^- which is your original query. ^-这是您的原始查询。

If your "dateofb" field is a DATE or DATETIME: 如果您的“ dateofb”字段是DATE或DATETIME:

SELECT 
    *    
FROM biography
WHERE dateofb >= (CURDATE() - INTERVAL 90 DAY)
ORDER BY MONTH(dateofb), DAY(dateofb) ASC
LIMIT 0, 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM