多个日期列按第一个即将到来的日期Mysql的顺序


Multiple date columns in order of first upcoming date Mysql

我有一个问题。多个Dj在我们的收音机里做音乐,但我们想做一个时间表,所以我们做了很多关于Dj的信息栏,但也包含12个日期时间戳在12个不同的列。

问题是,我如何选择第一个即将到来的日期?或者我如何选择日期比当前日期高的第一列。

如果这个问题解决了,我就会遇到另一个问题,因为它下面的脚本如何知道我使用了哪个变量。

我使用了以下变量和列:$date1, $date2, $date3等。

谢谢!

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

通常,我会通过执行普通的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