in the dni
table - selected days of the week (0 - Monday, 1 - Tuesday, etc.).
in the period
table - 30 examples of time intervals (the starting day is included in the calculation, the final day is not included).
in the final query, the number of Mondays and Fridays between dates.
the function that performs the calculation can probably be further simplified:
SQL feeddle
MySQL 5.6 Schema Setup :
create table dni (id int, d int); create table period (id int, s date, p date); insert into dni values (1, 0) # пн ,(1, 4) # пт ; insert into period values (1, '2015-01-01', '2015-01-02') ,(1, '2015-01-01', '2015-01-03') ,(1, '2015-01-01', '2015-01-04') ,(1, '2015-01-01', '2015-01-05') ,(1, '2015-01-01', '2015-01-06') ,(1, '2015-01-01', '2015-01-07') ,(1, '2015-01-01', '2015-01-08') ,(1, '2015-01-01', '2015-01-09') ,(1, '2015-01-01', '2015-01-10') ,(1, '2015-01-01', '2015-01-11') ,(1, '2015-01-01', '2015-01-12') ,(1, '2015-01-01', '2015-01-13') ,(1, '2015-01-01', '2015-01-14') ,(1, '2015-01-01', '2015-01-15') ,(1, '2015-01-01', '2015-01-16') ,(1, '2015-01-01', '2015-01-17') ,(1, '2015-01-01', '2015-01-18') ,(1, '2015-01-01', '2015-01-19') ,(1, '2015-01-01', '2015-01-20') ,(1, '2015-01-01', '2015-01-21') ,(1, '2015-01-01', '2015-01-22') ,(1, '2015-01-01', '2015-01-23') ,(1, '2015-01-01', '2015-01-24') ,(1, '2015-01-01', '2015-01-25') ,(1, '2015-01-01', '2015-01-26') ,(1, '2015-01-01', '2015-01-27') ,(1, '2015-01-01', '2015-01-28') ,(1, '2015-01-01', '2015-01-29') ,(1, '2015-01-01', '2015-01-30') ,(1, '2015-01-01', '2015-01-31') ;
Query 1 :
select date_format(s,'%Y-%m-%d, %a') as 'начало', date_format(p,'%Y-%m-%d, %a') as 'конец', sum( ceil((ps)/7)-1+ if((weekday(p)>weekday(s) and (weekday(s)<=d and weekday(p)>d)) or (weekday(p)<=weekday(s) and (weekday(s)<=d or weekday(p)>d)), 1,0) ) as 'дней' from period, dni group by s, p order by s, p
Results :
| начало | конец | дней | |-----------------|-----------------|------| | 2015-01-01, Thu | 2015-01-02, Fri | 0 | | 2015-01-01, Thu | 2015-01-03, Sat | 1 | | 2015-01-01, Thu | 2015-01-04, Sun | 1 | | 2015-01-01, Thu | 2015-01-05, Mon | 1 | | 2015-01-01, Thu | 2015-01-06, Tue | 2 | | 2015-01-01, Thu | 2015-01-07, Wed | 2 | | 2015-01-01, Thu | 2015-01-08, Thu | 2 | | 2015-01-01, Thu | 2015-01-09, Fri | 2 | | 2015-01-01, Thu | 2015-01-10, Sat | 3 | | 2015-01-01, Thu | 2015-01-11, Sun | 3 | | 2015-01-01, Thu | 2015-01-12, Mon | 3 | | 2015-01-01, Thu | 2015-01-13, Tue | 4 | | 2015-01-01, Thu | 2015-01-14, Wed | 4 | | 2015-01-01, Thu | 2015-01-15, Thu | 4 | | 2015-01-01, Thu | 2015-01-16, Fri | 4 | | 2015-01-01, Thu | 2015-01-17, Sat | 5 | | 2015-01-01, Thu | 2015-01-18, Sun | 5 | | 2015-01-01, Thu | 2015-01-19, Mon | 5 | | 2015-01-01, Thu | 2015-01-20, Tue | 6 | | 2015-01-01, Thu | 2015-01-21, Wed | 6 | | 2015-01-01, Thu | 2015-01-22, Thu | 6 | | 2015-01-01, Thu | 2015-01-23, Fri | 6 | | 2015-01-01, Thu | 2015-01-24, Sat | 7 | | 2015-01-01, Thu | 2015-01-25, Sun | 7 | | 2015-01-01, Thu | 2015-01-26, Mon | 7 | | 2015-01-01, Thu | 2015-01-27, Tue | 8 | | 2015-01-01, Thu | 2015-01-28, Wed | 8 | | 2015-01-01, Thu | 2015-01-29, Thu | 8 | | 2015-01-01, Thu | 2015-01-30, Fri | 8 | | 2015-01-01, Thu | 2015-01-31, Sat | 9 |
Disclaimer: I did not register the relationship between the tables as unnecessary to demonstrate the operation of the function.
SELECT COUNT(*) FROM sched WHERE wdate BETHWEEN :d1 AND :d2
- artoodetoo