1

有没有办法检索与给定日期变量最接近的日期的记录?

例如:

今天的日期是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 
4

4 回答 4

1

只需选择日期大于的位置:

SELECT * FROM table WHERE STR_TO_DATE(yourdatecol, '%l, %d%S %F %Y') >= NOW() ORDER BY STR_TO_DATE(yourdatecol, '%l, %d%S %F %Y') LIMIT 1;

应该可以,但未经测试

编辑:将 STR_TO_DATE 格式更改为%l, %d%S %F %Y

于 2013-01-10T04:03:35.980 回答
1

采用

SELECT * FROM table WHERE yourdatecol >= NOW() order by yourdatecol LIMIT 1;
  • order by会给你最新的日期
  • limit将限制你只有一排。
于 2013-01-10T04:08:24.500 回答
1

试试这个怎么样:

SELECT
    a.*
FROM
    `bowl-track_fixtures` a
JOIN
    (
    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') >= 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);

这将返回夹具的最近和更大(“下一个”)日期。如果有多个相同的日期,它将返回它们。

我做了两个假设,所以你可能需要稍微调整一下:

  1. 字符串的日期格式。确保这是正确的,否则您可能会得到 0 条记录。

  2. 您确定“下一个”日期的粒度。我认为它是在 ,fixture_id因为日期被命名为fixture_date

于 2013-01-10T05:01:49.623 回答
0

试试这个

SELECT
    DATE_FORMAT(STR_TO_DATE(t.datestring, '%d/%m/%Y'), '%Y-%m-%d') AS ORDER_DATE ,*
FROM t 
WHERE
       DATE_FORMAT(STR_TO_DATE(t.datestring, '%d/%m/%Y'), '%Y-%m-%d') >= NOW()
ORDER BY
       ORDER_DATE DESC
LIMIT 1;

有关更多信息,请参阅 ==> http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format

测试代码

-- --------------------------------------------------------

--
-- Table structure for table `dates`
--

CREATE TABLE IF NOT EXISTS `dates` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `date` varchar(200) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;

--
-- Dumping data for table `dates`
--

INSERT INTO `dates` (`id`, `date`) VALUES
(1, 'Wednesday, 30th January 2013'),
(2, 'Wednesday, 30th January 2013'),
(3, 'Wednesday, 30th January 2013'),
(4, 'Wednesday, 06th February 2013'),
(5, 'Wednesday, 06th February 2013'),
(6, 'Wednesday, 06th February 2013');

<?php

    $connection = mysql_connect('localhost','root','')or die(mysql_error());
    $database   = mysql_select_db('stackoverflow')or die(mysql_error());

    $query = "SELECT
                STR_TO_DATE(date, '%W, %D %M %Y') AS ORDER_DATE,dates.*
            FROM 
                dates
            WHERE
                STR_TO_DATE(date, '%W, %D %M %Y') >= NOW()
            ORDER BY
                ORDER_DATE ASC
            LIMIT 1;";
    $result = mysql_query($query)or die(mysql_error());
    echo "<table width='100%' border='1'><tr><td>ID</td><td>DATE</td></tr>";
    while($row = mysql_fetch_assoc($result)){
        echo "<tr><td>".$row['id']."</td><td>".$row['ORDER_DATE']."</td></tr>";
    }
    echo "</table>";
?>

不要忘记在 mysql 日期字符串中添加年份。

于 2013-01-10T04:14:08.177 回答