0

我正在研究运输计划并在所选日期之前显示任务我正在使用以下 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'

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

4

1 回答 1

4

如果您想单独访问它们,切勿在单个字段中存储多个信息。将 CSV 列表放入字段是灾难的秘诀。

规范的方法是有一个可连接的taskid和显示它们的天数,然后将你的解析AND dayofweek('$date') <> '1' AND dayofweek('$date') <> '7'为一个 Join

于 2013-06-12T10:04:57.020 回答