Your issue is with this expression:
DATE(NOW()) - 7
When you try to subtract 7
from a date, MySQL converts the date to its integer representation (in this case I presume it was 20210107
) and then subtract 7 from it, giving 20210100
. It then tries to compare this to a datetime column and fails, since 20210100
is not a valid date. The code works when you use 6
because you end up with 20210101
, which is valid. What you should be doing instead is subtracting an interval (see the manual) so that you use date arithmetic, not integer arithmetic:
CURDATE() - INTERVAL 7 DAY
Note that CURDATE()
is equivalent to DATE(NOW())
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…