-2

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

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

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

4

1 回答 1

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
于 2013-07-19T06:17:32.083 回答