SQL:计算两个日期之间的天数[重复]

问题描述 投票:3回答:1

这个问题在这里已有答案:

我在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)

但我不知道怎么做。也许有人可以帮助我。

mysql date between
1个回答
1
投票
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
© www.soinside.com 2019 - 2024. All rights reserved.