2012-12-30 25 views
0

我遇到問題。多個DJ在我們的收音機上製作音樂,但我們想製作一個時間表,所以我們製作了許多有關DJ信息的專欄,但也包含十二個不同欄目的十二個日期時間郵票。按照即將到來的日期排列多個日期列Mysql

問題是,如何選擇第一個即將到來的日期?或者,如何選擇第一列的日期高於當前日期。還有以下幾個。

如果解決了這個問題,我還有另一個問題,因爲它下面的腳本如何知道我使用了哪個變量。

我用下面的變量和列:$ DATE1,DATE2 $,$ DATE3等

THX!

+0

你可以重構你的數據庫? –

+0

如果你可以粘貼你的表格結構,我們可以幫助你更好 – m4t1t0

回答

0
SELECT * 
FROM (
    SELECT dj, 'date1' whichdate, if (date1 >= now(), date1, null) nextdate 
    union 
    SELECT dj, 'date2' whichdate, if (date2 >= now(), date2, null) nextdate 
    union 
    SELECT dj, 'date3' whichdate, if (date3 >= now(), date3, null) nextdate 
    union 
    SELECT dj, 'date4' whichdate, if (date4 >= now(), date4, null) nextdate 
    union 
    SELECT dj, 'date5' whichdate, if (date5 >= now(), date5, null) nextdate 
    union 
    SELECT dj, 'date6' whichdate, if (date6 >= now(), date6, null) nextdate 
    union 
    SELECT dj, 'date7' whichdate, if (date7 >= now(), date7, null) nextdate 
    union 
    SELECT dj, 'date8' whichdate, if (date8 >= now(), date8, null) nextdate 
    union 
    SELECT dj, 'date9' whichdate, if (date9 >= now(), date9, null) nextdate 
    union 
    SELECT dj, 'date10' whichdate, if (date10 >= now(), date10, null) nextdate 
    union 
    SELECT dj, 'date11' whichdate, if (date11 >= now(), date11, null) nextdate 
    union 
    SELECT dj, 'date12' whichdate, if (date12 >= now(), date12, null) nextdate 
) 
WHERE nextdate IS NOT NULL 
ORDER BY dj, nextdate 
+0

Thx男人!但是,你的意思是什麼意思? –

+0

您問「它下面的腳本如何知道我使用了哪個變量?」。該專欄告訴它它是哪一個。如果對模式進行規範化,這可能會更好,因此這些是不同的行。那麼你不需要這樣做。 – Barmar

0

通常我會從數據庫中獲取下一個(即將到來的)日期,方法是執行正常的SELECT語句,但將結果限制爲「1」,並使用WHERE子句獲取日期,並確保結果按順序排序按日期。

在我下面的例子中,我從DB中選擇了下一個日期的記錄,其中可能還包括今天的日期。如果您不關心今天的記錄,那麼只需將select命令中的'newtime'變量更改爲'todaydate'即可。

$todaydate = date('Y-m-d'); 
$newdate = strtotime ('-1 day' , strtotime ($todaydate)) ; 
$newdate = date ('Y-m-j' , $newdate); 

SELECT * FROM table WHERE date > '$newdate' ORDER BY table.`date`ASC LIMIT 1 
+0

他的問題的整個觀點是有12個日期列,而不僅僅是1.這個答案如何解決這個問題? – Barmar