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

时间:2012-12-30 00:30:12

标签: php mysql

我有问题。多个DJ正在我们的收音机上制作音乐,但是我们想制作一个时间表,所以我们制作了很多关于DJ的信息栏目,还包含十二个不同列中的十二个日期时间戳。

问题是,如何选择第一个即将到来的日期?或者如何选择第一列哪个日期高于当前日期。还有以下几个。

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

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

THX!

2 个答案:

答案 0 :(得分:0)

通常我会通过执行正常的SELECT语句从数据库中获取下一个(即将到来的)日期,但将结果限制为" 1"并使用WHERE子句获取日期,同时确保按日期按顺序对结果进行排序。

在下面的示例中,我从DB中选择了下一个日期的记录,其中也可能包含今天的日期。如果你不关心今天的记录,那么只需改变“新时间”即可。 select命令中的变量为' 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

答案 1 :(得分: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
相关问题