I need to get the previous weekday. So when it's monday I need to get the result friday but I can't seem to understand the WEEKDAY() function. Could someone help to start with this?

有帮助吗?

解决方案

WEEKDAY() returns 0 to 6 depending on which day of the week it is. You could put logic in the code to ignore weekend conditions, and go back to the Friday.

Returns the weekday index for date (0 = Monday, 1 = Tuesday, … 6 = Sunday).

So if WEEKDAY(date) - 1 == 5 || WEEKDAY(date) - 1 == 6 then make it equal 4 (Friday) instead.

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_weekday

其他提示

Try following

select date_add(curdate(), interval
case weekday(curdate())
   when 0 then 5
   when 6 then -2
   else -1
end 
day);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top