-1

我有一周中所有日子的数据,我想查找最近工作日的数据,即dayofweek != 1 and dayofweek != 7

我笨拙的 WHERE with CASE 类似于

WHERE CASE WHEN dayofweek(curdate()) = 1 THEN day(time) = date_sub(day(time), interval 2 day) 
    WHEN dayofweek(curdate()) = 7 THEN day(time) = date_sub(day(time), interval 1 day)  
    WHEN dayofweek(curdate()) != 7 AND dayofweek(curdate()) != 1 THEN day(time) = day(curdate())  ELSE 1 = 1  END
4

1 回答 1

1

此代码day(time) = date_sub(day(time), interval 2 day)从不匹配,因为day(time) 从不等于day(time) - 1 day。这与代数中的说法相同x=x-1......它根本不可能是真的。我怀疑你实际上是打算做这样的事情:

WHERE CASE WHEN dayofweek(curdate()) = 1 THEN date(time) = date(date_sub(curdate(), interval 2 day)) 
...
...
... This part was left as an exercise for the asker.

date()功能删除了时间部分,因此您可以只比较日期。 The date_sub()函数需要减去curdate()以获得最近的工作日。DATETIME在您的代码中,它从表中存储的值中减去。

于 2013-05-14T18:41:50.593 回答