找出在过去的x天内是否没有创建任何记录

时间:2011-09-09 17:02:12

标签: mysql if-statement max intervals

假设我有一个数据库来跟踪浇灌植物,其结构如下:

Table: "Plants"
ID     Plants
----------------------------------
1      Roses
2      Daisies
3      Sunflowers
4      Marigolds
5      Daffodils

Table: "Maintenance"
ID    Plant_ID    Activity    Date
-------------------------------------
1     1            Water      2011-09-09
2     1            Water      2011-08-02
3     2            Water      2011-08-15
4     3            Water      2010-07-01
5     4            Weed       2010-07-01

我正在尝试编写一个查询,告诉我们植物是否需要浇水,如果需要浇灌,会有多糟糕。换句话说:如果植物在过去30天内没有浇水,则返回警告水平“黄色”;如果在过去60天内没有浇水,则为“红色”警告级别。

到目前为止我在这里:

SELECT Plants.Plants, 
IF ( DATEDIFF(CURRENT_DATE, MAX(Maintenance.Date)) < 30 AND maintenance.type = "Water", '',
  IF ((DATEDIFF(CURRENT_DATE, MAX(Maintenance.Date)) >= 60 AND maintenance.type = "Water") or maintenance.date IS NULL, 'Red', 'Yellow')
) AS `Water Warning`,
IF ( DATEDIFF(CURRENT_DATE, MAX(Maintenance.Date)) < 30 and maintenance.type = "Weed" , '',
  IF ((DATEDIFF(CURRENT_DATE, MAX(Maintenance.Date)) >= 60 and maintenance.type = "Weed") or maintenance.date IS NULL, 'Red', 'Yellow')
) AS `Weeding Warning`
FROM `plants`
LEFT JOIN `maintenance` ON `maintenance`.`plant_id` = `plants`.`id`
GROUP BY `plants`.`id`;

但是这会为“Roses”返回“Red”警告,而不是预期的空字符串,并且没有Maintenance.Activity的条件。请注意,同样的查询还需要为Weeding和其他活动执行类似的功能,因此在WHERE子句中过滤可能不是答案。

这就是我追求的目标:

Results
ID     Plants        "Water Warning"  "Weed Warning"
----------------------------------------------------
1      Roses               ""             "Red"
2      Daisies           "Yellow"         "Red"
3      Sunflowers         "Red"           "Red"
4      Marigolds          "Red"            ""
5      Daffodils          "Red"           "Red"

3 个答案:

答案 0 :(得分:1)

使用下面的语句,您只需要将表格与植物名称一起加入。

SELECT *, 

CASE
    WHEN DATEDIFF(CURRENT_DATE, DATE) >= 60 THEN 'RED'
    WHEN DATEDIFF(CURRENT_DATE, DATE) >= 30 THEN 'YELLOW'
END AS FLAG
FROM test_table

答案 1 :(得分:0)

我真的希望有一个更优雅的解决方案,但这有效:

SELECT 
`plants`.`plants`,
IF((MAX(IF(`maintenance`.`type` = 'Water',
        `maintenance`.`date`,
        NULL)) IS NULL) OR ((DATEDIFF(CURRENT_DATE,
            MAX(IF(`maintenance`.`type` = 'Water',
                `maintenance`.`date`,
                NULL)))) > 30),
    (IF((MAX(IF(`maintenance`.`type` = 'Water',
            `maintenance`.`date`,
            NULL)) IS NULL) OR ((DATEDIFF(CURRENT_DATE,
                MAX(IF(`maintenance`.`type` = 'Water',
                    `maintenance`.`date`,
                    NULL)))) > 90),
        'red',
        'yellow')),
    '') AS `Water Warning`,
IF((MAX(IF(`maintenance`.`type` = 'Weed',
        `maintenance`.`date`,
        NULL)) IS NULL) OR ((DATEDIFF(CURRENT_DATE,
            MAX(IF(`maintenance`.`type` = 'Weed',
                `maintenance`.`date`,
                NULL)))) > 30),
    (IF((MAX(IF(`maintenance`.`type` = 'Weed',
            `maintenance`.`date`,
            NULL)) IS NULL) OR ((DATEDIFF(CURRENT_DATE,
                MAX(IF(`maintenance`.`type` = 'Weed',
                    `maintenance`.`date`,
                    NULL)))) > 90),
        'red',
        'yellow')),
    '') AS `Weed Warning`
FROM
`plants`
    LEFT JOIN
`maintenance` ON `maintenance`.`plant_id` = `plants`.`id`
GROUP BY `plants`.`id`;

答案 2 :(得分:-1)

实际数据

mysql> select * from Plants;
+------+------------+
| ID   | Plants     |
+------+------------+
|    1 | Roses      |
|    2 | Daisies    |
|    3 | Sunflowers |
|    4 | Marigolds  |
|    5 | Daffodils  |
+------+------------+

mysql> select * from Maintenance;
+----+----------+----------+------------+
| ID | Plant_ID | Activity | Date       |
+----+----------+----------+------------+
|  1 |        1 | Water    | 2011-09-09 |
|  2 |        1 | Water    | 2011-08-02 |
|  3 |        2 | Water    | 2011-08-15 |
|  4 |        3 | Water    | 2010-07-01 |
|  5 |        4 | Weed     | 2010-07-01 |
+----+----------+----------+------------+
SELECT
Plants.Plants,
COALESCE(
CASE mt.Activity='Weed'
  WHEN 1 THEN
    CASE DATEDIFF(CURRENT_DATE, mt.last_date) BETWEEN 0 AND 30
      WHEN 1 THEN ''
      ELSE
        CASE DATEDIFF(CURRENT_DATE, mt.last_date) BETWEEN 30 AND 60
        WHEN 1 THEN 'Yellow'
        ELSE NULL
        END
    END
END, 'Red') AS "Weed Warning",
COALESCE(
CASE mt.Activity='Water'
  WHEN 1 THEN
    CASE DATEDIFF(CURRENT_DATE, mt.last_date) BETWEEN 0 AND 30
      WHEN 1 THEN ''
      ELSE
        CASE DATEDIFF(CURRENT_DATE, mt.last_date) BETWEEN 30 AND 60
        WHEN 1 THEN 'Yellow'
        ELSE NULL
        END
    END
END, 'Red') AS "Water Warning"
FROM Plants
LEFT JOIN
( SELECT Plant_ID, Activity, MAX(Date) AS last_date
  FROM Maintenance
  GROUP BY Plant_ID, Activity) AS mt ON Plants.ID=mt.Plant_ID
GROUP BY Plants.ID;

<强>结果

+------------+--------------+---------------+
| Plants     | Weed Warning | Water Warning |
+------------+--------------+---------------+
| Roses      | Red          |               |
| Daisies    | Red          |               |
| Sunflowers | Red          | Red           |
| Marigolds  | Red          | Red           |
| Daffodils  | Red          | Red           |
+------------+--------------+---------------+

OP备注

  1. 您发布了错误的结果
  2. 您已引用不存在列type
  3. 您没有注意表名称区分大小写