更改mysql查询以仅在指定日期显示任务

时间:2013-06-12 10:01:24

标签: php mysql

我正在制作一个传输规划器,并在所选日期显示我正在使用以下mysql查询的任务:

    SELECT *
    FROM planner
    WHERE '$date' >= date
    AND '$date' <= valid_till
    AND (frequency = 'daily'
    OR (frequency = 'once' AND date('$date') = date)
    OR (frequency = 'weekly' AND 0 = (abs(datediff('$date', date)) % 7))
    OR (frequency = 'two-weekly' AND 0 = (abs(datediff('$date', date)) % 14))
    OR (frequency = 'monthly'
    AND ceil(dayofmonth('$date')/7) = ceil(dayofmonth(date)/7)
    AND dayofweek('$date') = dayofweek(date)))
    AND dayofweek('$date') <> '1'
    AND dayofweek('$date') <> '7'

现在我有一些任务只需要在星期一,星期三和星期四举例。 我以为我在我的计划表中创建了一个新列:only_on_days(varchar),我填写如下:'2,4,5',其中2表示星期一,4表示星期三,5表示星期四。

有人可以帮助我更改查询以添加此功能吗? 或者也许有更好的想法?

修改

我创建了一个新表: planner_days ,看起来像(示例):

id --- planner_id --- day
1          12          2
2          12          3
3          12          4
4          12          5
5          12          6
6          13          3
7          13          5

我试图像@ eugen-rieck那样建立一个连接查询。

SELECT planner.*, planner_days.*
FROM planner
LEFT JOIN planner_days
ON planner.planner_id = planner_days.planner_id
WHERE '$date' >= date
AND '$date' <= valid_till
AND (frequency = 'daily' AND dayofweek('$date') = planner_days.day
OR (frequency = 'once' AND date('$date') = date)
OR (frequency = 'weekly' AND 0 = (abs(datediff('$date', date)) % 7))
OR (frequency = 'two-weekly' AND 0 = (abs(datediff('$date', date)) % 14))
OR (frequency = 'monthly'
AND ceil(dayofmonth('$date')/7) = ceil(dayofmonth(date)/7)
AND dayofweek('$date') = dayofweek(date)))
AND dayofweek('$date') <> '1'
AND dayofweek('$date') <> '7'

这有效,但现在每天必须显示的任务会多次显示.. 有人有解决方案吗?

1 个答案:

答案 0 :(得分:4)

永远不会在单个字段中存储多个信息,如果您想单独访问。将CSV列表放入字段是desaster的一个秘诀。

规范方法是使用taskid的加入表和显示它们的日期,然后将AND dayofweek('$date') <> '1' AND dayofweek('$date') <> '7'解析为加入