Eine gute Technik dazu wird hier gezeigt – siehe Tipp #23. Sie legt eine kleine Hilfstabelle an. Ist diese erstellt, sieht deine Abfrage etwa so aus:
SELECT monthdate AS first_day_of_month, DATE_ADD(monthdate, INTERVAL( ( 10 - DAYOFWEEK(monthdate) ) % 7 ) + 7 DAY) AS second_saturday_of_month FROM monthdatesDie Rechnung ist der Kern: DAYOFWEEK() liefert 1 für Sonntag bis 7 für Samstag. Der Modulo-Anteil positioniert auf den ersten Samstag, das nachträgliche + 7 DAY verschiebt auf den zweiten.
Die Hilfstabelle lässt sich in MySQL 8 ohne Workaround direkt erzeugen:
WITH RECURSIVE monthdates AS ( SELECT '2024-01-01' AS monthdate UNION ALL SELECT DATE_ADD(monthdate, INTERVAL 1 MONTH) FROM monthdates WHERE monthdate < '2024-12-01' ) SELECT monthdate, DATE_ADD(monthdate, INTERVAL (10 - DAYOFWEEK(monthdate)) % 7 + 7 DAY) AS second_saturday FROM monthdates;