Count days between two dates, excluding weekends (MySQL only)

后端 未结 5 591
离开以前
离开以前 2020-11-28 16:26

I need to calculate the difference (in days) between two dates in MySQL excluding weekends (Saturday and Sunday). That is, the difference in days minus the number of Saturda

5条回答
  •  醉梦人生
    2020-11-28 16:34

    You can use a dates table:

    tbl_dates

    dow is 'day of week' in my table. Then your query looks like this:

    SELECT Count(theDate) AS numWeekDays
    FROM tbl_dates
    WHERE theDate >[startDate] And theDate <=[endDate] AND dow <> 1 AND dow <> 7;
    

    In this case, 1 and 7 are Sunday, Saturday, respectively (which is the default) and, of course, you can nest that into another query if you need to calculate this for many startDate(s) and endDate(s).

提交回复
热议问题