MYSQL直前の日曜日は何日か



 SELECT CURDATE() , 
 DATE_FORMAT(ADDDATE(NOW(), 0), "%w") AS today,
CASE 0  -- 月曜にしたければ1
 WHEN DATE_FORMAT(ADDDATE(NOW(), 0), "%w")  THEN ADDDATE(CURDATE(), 0)
 WHEN DATE_FORMAT(ADDDATE(NOW(), -1), "%w") THEN ADDDATE(CURDATE(), -1)
 WHEN DATE_FORMAT(ADDDATE(NOW(), -2), "%w") THEN ADDDATE(CURDATE(), -2)
 WHEN DATE_FORMAT(ADDDATE(NOW(), -3), "%w") THEN ADDDATE(CURDATE(), -3)
 WHEN DATE_FORMAT(ADDDATE(NOW(), -4), "%w") THEN ADDDATE(CURDATE(), -4)
 WHEN DATE_FORMAT(ADDDATE(NOW(), -5), "%w") THEN ADDDATE(CURDATE(), -5)
 WHEN DATE_FORMAT(ADDDATE(NOW(), -6), "%w") THEN ADDDATE(CURDATE(), -6)
END AS start_week -- 直前の日曜日


Categories:

Tags: