mysql在给出之后找到下一个日期

时间:2013-01-10 04:00:02

标签: php mysql

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

例如:

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

然而,这不会返回结果

@Nicarus

这是我正在使用的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 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

使用

SELECT * FROM table WHERE yourdatecol >= NOW() order by yourdatecol LIMIT 1;
  • order by将为您提供最新日期
  • limit会将您限制为只有一行。

答案 2 :(得分: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

答案 3 :(得分: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日期字符串中添加年份。