0

我希望下面的查询根据日期范围命名案例列 - 但是所有内容都默认为错误 - 我错过了什么?自从我在 mySQL 中工作以来已经有一段时间了。我正在尝试根据过期的时间间隔进行分组,今天,明天,30 天,60 天,90 天。

    SELECT DATE_SUB(CURDATE(), INTERVAL 1 DAY)AS rDate,

    CURDATE()AS rDate,DATE_ADD(CURDATE(), INTERVAL 1 DAY)AS rDate,

    DATE_ADD(CURDATE(), INTERVAL 7 DAY)AS rDate,

    DATE_ADD(CURDATE(), INTERVAL 14 DAY)AS rDate,

    DATE_ADD(CURDATE(), INTERVAL 30 DAY)AS rDate,

    DATE_ADD(CURDATE(), INTERVAL 60 DAY)AS rDate,

    DATE_ADD(CURDATE(), INTERVAL 90 DAY)AS rDate,

    CASE a.nextDueDate

    WHEN a.nextDueDate < CURDATE() THEN 'Overdue'

    WHEN a.nextDueDate > CURDATE() AND a.nextDueDate < (DATE_ADD(CURDATE(), INTERVAL 7 
    DAY))THEN '7 Days'

    WHEN a.nextDueDate > CURDATE() AND a.nextDueDate < (DATE_ADD(CURDATE(), INTERVAL 
    14 DAY))THEN '15 Days'

    WHEN a.nextDueDate > CURDATE() AND a.nextDueDate < (DATE_ADD(CURDATE(), INTERVAL 30 DAY))THEN '30 Days'

    WHEN a.nextDueDate > CURDATE() AND a.nextDueDate < (DATE_ADD(CURDATE(), INTERVAL 60 DAY))THEN '60 Days'

    WHEN a.nextDueDate > CURDATE() AND a.nextDueDate < (DATE_ADD(CURDATE(), INTERVAL 90 DAY))THEN '90 Days'

    ELSE 'Error'

    END,

    g.groupId,g.name AS groupName,l.logId,l.name AS logName, i.itemID,

    i.name AS itemName, le.userName,completed, i.optimalMin,i.optimalMax ,le.value,

    u.name AS unitDescription,

    g1.parentId AS parentId1, g1.name AS group1Name,g2.parentId AS parentId2, g2.name 
    AS group2Name,

    g3.parentId AS parentId3, g3.name AS group3Name,

    a.name AS activityName,scheduleType,scheduleInterval,a.details,nextDueDate

    FROM logExceptions le

    INNER JOIN logs l ON l.logID = le.logID

    INNER JOIN groups g ON g.groupId = l.groupId

    INNER JOIN items i ON le.itemId = i.itemId

    INNER JOIN activities a ON l.logId = a.logId

    LEFT JOIN units u ON i.unitId = u.unitId

    LEFT JOIN groups g1 ON g.parentId = g1.groupId

    LEFT JOIN groups g2 ON g1.parentId = g2.groupId

    LEFT JOIN groups g3 ON g2.parentId = g3.groupId

    WHERE  nextDueDate IS NOT NULL
4

1 回答 1

2

a.nextDueDateCASE关键字后面删除。

表达式有CASE两种形式:

CASE <expression>
    WHEN <test-value1> THEN <result-value1>
    WHEN <test-value2> THEN <result-value2>
    ...
    ELSE <else-value>
END

在这种形式中,将 的值<expression>与 each 进行比较<test-valueN>,当匹配时,<result-valueN>返回对应的值。如果没有匹配,<else-value>则返回 。这种形式类似于其他编程语言中的switchorcase语句。

另一种形式是:

CASE WHEN <test-expression1> THEN <result-value1>
     WHEN <test-expression2> THEN <result-value2>
     ...
     ELSE <else-value>
END

在这种情况下,每个都<test-expressionN>被评估,如果一个为真,<result-valueN>则返回对应的。如果没有一个为真,<else-value>则返回 。这类似于if/then/elseif/else其他编程语言。

由于您使用的是后一种类型CASE,因此您不应该在 . 之后使用表达式CASE

于 2013-10-29T05:12:41.177 回答