我有一周中所有日子的数据,我想查找最近一个工作日的数据,dayofweek != 1 and dayofweek != 7
我的笨拙与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
答案 0 :(得分: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
中减去。