2013-01-10 16 views
1

有沒有辦法檢索到最近日期的記錄到給定的日期變量?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 
+0

您的日期是實際日期,還是他們的字符串格式? – Ryan

+2

最近或最近的AND更大的日期? – 2013-01-10 04:02:17

+0

字符串格式我使用strtotime轉換我的php文件 –

回答

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

+0

我收到以下'函數db.STR_TO_DATE不存在' –

+0

您正在使用MySQL,對嗎?它是否給出了錯誤信息的行號? – 2013-01-10 05:21:11

+0

請參閱我的更新請 –

1

只需選擇d吃大於:

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

添加一個訂單通過?? – 2013-01-10 04:05:29

+0

@keeg謝謝,快速的問題是這麼簡單,因爲我的日期存儲爲字符串? –

+1

@DavidPassmore - 簡單地將字符串轉換爲日期... – 2013-01-10 04:08:33

1

使用

SELECT * FROM table WHERE yourdatecol >= NOW() order by yourdatecol LIMIT 1; 
  • order by會給你最新日期
  • limit將限制您只有一行。
+3

爲什麼按降序排列? – 2013-01-10 04:09:36

+0

是的,我的錯誤... – sourcecode

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 datestring增加一年。

+0

是的,但你的'ORDER BY'是一個字符串值,而不是日期,所以它可能不會正確排序。 – 2013-01-10 04:14:53

+0

請看更新的答案.. –

+0

@DipeshParmar謝謝,但這沒有結果返回... :( –

相關問題