今天是X号
a = x % 7, 求余
b = floor(X / 7) , x/7 然后取整
如果a==0, 结果为b,否则
c = 本月1号是周几, 计算从1号 到62616964757a686964616fe4b893e5b19e31333335303465a号是否包含周二如果包含,结果为b+1
否则,结果为b
最终的SQL语句为:
SELECT
if(dayofmonth(now()) % 7=0, dayofmonth(now())/7,
if((1 between weekday(date_sub(now(), interval dayofmonth(now())-1 day)) and weekday(date_sub(now(), interval dayofmonth(now())+dayofmonth(now())%7-1 day))) or
(8 between weekday(date_sub(now(), interval dayofmonth(now())-1 day)) and weekday(date_sub(now(), interval dayofmonth(now())+dayofmonth(now())%7-1 day))), floor(dayofmonth(now())/7)+1, floor(dayofmonth(now())/7))
);
如果使用变量改写一下SQL,看起来更简单一些:
set @a=dayofmonth(now()) % 7;
set @b=dayofmonth(now()) / 7;
set @c=weekday(date_sub(now(), interval dayofmonth(now())-1 day));
set @d=weekday(date_sub(now(), interval dayofmonth(now())+@a-1 day));
SELECT
if(@a=0, @b,
if((1 between @c and @d) or (8 between @c and @d), floor(@b)+1, floor(@b))
) as result;