这个问题在这里已有答案:
我在MySQL数据库中有一个表,其中包含以下字段:
Name From_Date To_Date
Mr. Spencer 2018-09-01 2018-09-25
我喜欢得到他工作的星期一。例如,结果必须是4. 2018-09-01到2018-09-25,那里的4个星期一(09-03,09-10,09-17,09-24)
但我不知道怎么做。也许有人可以帮助我。
SELECT
(DATEDIFF('2018-09-25', '2018-09-01') # Number of days between start and end
+ 7 # Add a week to account for first Monday
- (9 - DAYOFWEEK('2018-09-01')) % 7) # Days between start and next Monday
# (0 if the provided date is a Monday)
# 9 because DAYOFWEEK() returns 2 for Mon
DIV 7; # Divide by seven and ignore the remainder