'How to calculate weekends of time range using MySQL

I need to calculate weekends of time range using MySQL.

This is what I have so far(about calculate working days but i want to calculate weekends):

select
    (floor(days / 7)* 5
    + days%7
    - case
        when 6 between wd and wd + days%7 - 1 then 1
        else 0
    end
    - case
        when 7 between wd and wd + days%7 - 1 then 1
        else 0
    end) as result
from
    (
    select
        abs(datediff('2022-05-15', '2022-05-13')) + 1 as days,
        weekday('2022-05-13') + 1 as wd ) as a;

Hope I explained well...

Thanks.



Solution 1:[1]

select
(floor(days / 7)* 2
+ case
    when 6 between wd and wd + days%7 - 1 then 1
    else 0
end
+ case
    when 7 between wd and wd + days%7 - 1 then 1
    else 0
end) as result 

from ( select abs(datediff('2022-05-15', '2022-05-13')) + 1 as days, weekday('2022-05-13') + 1 as wd ) as a;

Maybe this will work. Have a try.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Li Yong