如何在mysql中从当前月份计算上个月的上个星期五

时间:2013-07-19 05:38:37

标签: mysql

我需要从当月开始计算上个月的最后一个星期四

set @ldom = dayofweek(last_day(curdate()));

select     
case
    when @ldom = 7 then last_day(curdate()) - interval 1 day
    when @ldom = 6 then last_day(curdate())
    when @ldom = 5 then last_day(curdate()) - interval 6 day 
    when @ldom = 4 then last_day(curdate()) - interval 5 day 
    when @ldom = 3 then last_day(curdate()) - interval 4 day
    when @ldom = 2 then last_day(curdate()) - interval 3 day
    else last_day(curdate()) - interval 6 day
end as last_friday

它给了我当月的最后一个星期五,但我需要计算上个月的最后一个星期五

1 个答案:

答案 0 :(得分:1)

如果你想获得最后一个星期四,那就试试吧。 http://sqlfiddle.com/#!2/d41d8/16634

SELECT case weekday(last_day(curdate()-interval 1 month)) 
when 6 then date_sub(last_day(curdate()-interval 1 month),interval 3 day)
when 5 then date_sub(last_day(curdate()-interval 1 month),interval 2 day)
when 4 then date_sub(last_day(curdate()-interval 1 month),interval 1 day)
when 3 then date_sub(last_day(curdate()-interval 1 month),interval 0 day)
when 2 then date_sub(last_day(curdate()-interval 1 month),interval 4 day)
when 1 then date_sub(last_day(curdate()-interval 1 month),interval 5 day)
when 0 then date_sub(last_day(curdate()-interval 1 month),interval 6 day) end day