我正在研究运输规划师.我的
MySQL(i)/
PHP知识是(非常)基础的.
我想显示一个HTML表,其中包含某一天的所有任务.
但问题是需要重复某些任务(直到我删除整个任务),因此任务的频率如下:一次,每天,每周,每两周和每月.
所以当然,当我在星期一以每周频率添加新任务时,该任务必须在每个星期一返回我的表格.
我应该结合如下:
SELECT * FROM planner
WHERE frequency = `once`
AND date = CURDATE()
SELECT * FROM planner
WHERE frequency = `daily/weekly/two-weekly/monthly/yearly`
AND day = DAYOFWEEK(CURDATE())
AND date + INTERVAL 1 DAY/1 WEEK/2 WEEK/1 MONTH
如果是这样,那怎么样?
希望可以有人帮帮我.
编辑:
对于一次,每日和每周任务,这样的查询(可能仍然很可怕)将会:
(SELECT *
FROM planner
WHERE frequency = 'once'
AND date = NOW())
UNION
(SELECT *
FROM planner
WHERE frequency = 'daily'
AND date + INTERVAL 1 DAY)
UNION
(SELECT *
FROM planner
WHERE frequency = 'weekly'
AND day = DAYOFWEEK(CURDATE())
AND date + INTERVAL 1 WEEK)
虽然,对于“两周”和“每月”间隔2周/ 1个月,每周都会返回结果.如何解决这个问题?
编辑:
我不在表格中使用有效列,因为我不知道过期/无效的过去任务.我把我的笔记本电脑年改为2040年,确实有一个mysql错误.我已经改变了计划,我认为重复任务有效期为一年更好,可以选择删除或延长一年.然后我也忘了提到当我星期一以每月的频率创建一个任务时,该任务会在我的表中返回下个月的第一个星期一.当我尝试您的解决方案时,我的表中每天都会出现具有“一次”频率的任务.
编辑
@SparKot,通常以每月频率查看任务,例如2013-03-05,下一次将是2013-04-05,2013-05-05等.
虽然在这个计划中每月设定一个例子,2013-03-05星期二应该在下个月的第一个星期二重新出现(4月:2013-04-02,可能:2013-05-07,6月:2013-06- 04).
最佳答案 假设表中有有效列:
SELECT *
FROM planner
WHERE now() >= valid_from
AND now() <= valid_to
AND (frequency = 'daily'
OR (frequency = 'once' AND date(now()) = date)
OR (frequency = 'weekly' AND 0 = (abs(datediff(now(), date)) % 7))
OR (frequency = 'two-weekly' AND 0 = (abs(datediff(now(), date)) % 14))
OR (frequency = 'monthly'
AND ceil(dayofmonth(now())/7) = ceil(dayofmonth(date)/7)
AND dayofweek(now()) = dayofweek(date))
);
没有有效列:您可能会在过去尚未删除的任务中过期/无效:
SELECT *
FROM planner
WHERE (frequency = 'daily'
OR (frequency = 'once' AND date(now()) = date)
OR (frequency = 'weekly' AND 0 = (abs(datediff(now(), date)) % 7))
OR (frequency = 'two-weekly' AND 0 = (abs(datediff(now(), date)) % 14))
OR (frequency = 'monthly'
AND ceil(dayofmonth(now())/7) = ceil(dayofmonth(date)/7)
AND dayofweek(now()) = dayofweek(date))
);