我试图从当前日期订购结果
这就是我现在正在使用的东西;
SELECT * FROM friends JOIN bdays
ON bdays.user = friends.friendname
WHERE username = $userid ORDER BY DATE_FORMAT(date, '%m %d')
任何想法?
示例 现在按日期排序,从1月开始对生日进行排序
我需要的是,不是在1月份开始列表,而是从当前日期开始。
所以,而不是;
January
February
March
April
May
June
July
August
September
November
December
它会像这样命令他们;
April (current month/day)
May
June
July
August
September
November
December
January
February
March
April (all the way up to yesterday)
答案 0 :(得分:4)
我是这样做的:
SELECT *, (DATE_FORMAT(date, '%j')-DATE_FORMAT(NOW(), '%j')+365)%365 AS d
FROM foo ORDER BY d;
%j
日期格式是一年中的某一天,即数字001 ... 366。
我在一些样本数据上对此进行了测试,并按照您描述的方式对其进行排序:它忽略了年份,并对当前日期之后的下一个日期进行排序,然后按升序排序,并在一年中较早的日期进行排序。
+----+------------+------+
| id | date | d |
+----+------------+------+
| 5 | 1999-05-15 | 27 |
| 6 | 1992-06-15 | 59 |
| 7 | 1990-07-15 | 88 |
| 8 | 1988-08-15 | 120 |
| 9 | 1980-11-15 | 212 |
| 1 | 2010-01-15 | 272 |
| 2 | 2009-02-15 | 303 |
| 3 | 2004-03-15 | 332 |
| 4 | 2002-04-15 | 362 |
+----+------------+------+
答案 1 :(得分:2)
你可以尝试:
ORDER BY
DATE_FORMAT(date,'%m %d') < DATE_FORMAT(NOW(),"%m %d"),
DATE_FORMAT(date,'%m %d');
首先,按日期是否小于当前日期排序,然后按月和日期按升序排序。
注意强> 这看起来像是Col. Shrapnel所指的方法。
答案 2 :(得分:1)
类似于if(date_format(date,'%m%d')&lt; date_format(now(),'%m%d')),1,0),date_format(date,'%m%)的顺序d')
答案 3 :(得分:1)
SELECT *, DATE_FORMAT(date, '%m %d') as adate FROM friends JOIN bdays
ON bdays.user = friends.friendname
WHERE username = $userid
ORDER BY adate