有没有办法检索与给定日期变量最接近的日期的记录?
例如:
今天的日期是Thursday, 10th January 2012
我的数据库有一个灯具列表,我想从中列出下周三的“下周”灯具,即:Wednesday, 16th January 2012
如何获取文件以输出日期列中包含此内容的所有行?
提前致谢!
编辑:
这是我的带有示例数据的表 - bowl-track_fixtures
:
| fixture_id | league_id | fixture_team_1 | fixture_team_2 | fixture_date |
---------------------------------------------------------------------------------------
| 1 | 2 | 5 | 6 | Wednesday, 30th January |
| 2 | 2 | 4 | 1 | Wednesday, 30th January |
| 3 | 2 | 2 | 3 | Wednesday, 30th January |
| 1 | 2 | 5 | 6 | Wednesday, 06th February |
| 2 | 2 | 4 | 1 | Wednesday, 06th February |
| 3 | 2 | 2 | 3 | Wednesday, 06th February |
etc..
我希望系统只向我显示离今天日期最近和之后的行。
我尝试了以下方法;
SELECT * FROM `bowl-track_fixtures` WHERE STR_TO_DATE(`fixture_date`, '%l, %d%S %F %Y') >= NOW() ORDER BY STR_TO_DATE(`fixture_date`, '%l, %d%S %F %Y') LIMIT 1;
然而,这不返回任何结果
@尼卡洛斯
这是我正在使用的 MySQL 代码:
SELECT fixture_id, MIN( STR_TO_DATE(
fixture_date, '%l, %d%S %F %Y'
) ) AS `next_fixture_date`
FROM `bowl-track_fixtures`
WHERE STR_TO_DATE(
`fixture_date` , '%l, %d%S %F %Y'
) & gt ; = NOW( )
GROUP BY `fixture_id`
)b ON ( a.`fixture_id` = b.`fixture_id` )
AND (
STR_TO_DATE(
a.`fixture_date` , '%l, %d%S %F %Y'
) = b.`next_fixture_date`
)
LIMIT 0 , 30
它返回这个:
MySQL said:
#1305 - FUNCTION db.STR_TO_DATE does not exist