我有个问题。多个DJ在我们的收音机上制作音乐,但我们想制定一个时间表,所以我们制作了许多关于DJ的信息的专栏,但也在12个不同的栏目中包含了12个日期戳。
问题是,我如何选择第一个即将到来的日期?或者如何选择日期高于当前日期的第一列。还有下面的几个。
如果这个问题得到解决,我就会遇到另一个问题,因为它下面的脚本如何知道我使用的是哪个变量。
我使用了以下变量和列:$date1、$date2、$date3等。
谢谢!
发布于 2012-12-30 14:44:54
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发布于 2012-12-30 10:44:10
通常,我会通过执行普通的SELECT语句,但将结果限制为"1“,并使用WHERE子句来获取日期,同时确保结果按日期排序,从而从数据库中获取下一个(即将到来的)日期。
在下面的示例中,我从数据库中选择了具有下一个日期的记录,该日期也可能包括今天本身。如果您不关心今天的记录,那么只需将select命令中的'newtime‘变量更改为’today care‘即可。
$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 1https://stackoverflow.com/questions/14087231
复制相似问题