我需要每周(每个月的第一个星期五)进行一次活动,并且每个月的第一个星期五(每月的第一个星期五除外)都要进行一次活动。我所能想到的就是

IF WEEKDAY()=4 AND DAYOFMONTH() BETWEEN 1 AND 7 THEN ....




IF WEEKDAY()=4 AND DAYOFMONTH() BETWEEN 8 AND 31 THEN ....


但是,有没有更好/更简洁的查询呢?

最佳答案

SET @x := '2018-06-01';

SELECT CASE WHEN WEEKDAY(@x) = 4
            THEN CASE WHEN DAYOFMONTH(@x) < 8
                      THEN 'FIRST FRIDAY'
                      ELSE 'SOME OTHER FRIDAY' END
            ELSE 'NOT FRIDAY' END x;
+--------------+
| x            |
+--------------+
| FIRST FRIDAY |
+--------------+




SET @x := '2018-06-08';

SELECT CASE WHEN WEEKDAY(@x) = 4
            THEN CASE WHEN DAYOFMONTH(@x) < 8
                      THEN 'FIRST FRIDAY'
                      ELSE 'SOME OTHER FRIDAY' END
            ELSE 'NOT FRIDAY' END x;
+-------------------+
| x                 |
+-------------------+
| SOME OTHER FRIDAY |
+-------------------+

SET @x := '2018-06-07';

SELECT CASE WHEN WEEKDAY(@x) = 4
            THEN CASE WHEN DAYOFMONTH(@x) < 8
                 THEN 'FIRST FRIDAY'
                 ELSE 'SOME OTHER FRIDAY' END
            ELSE 'NOT FRIDAY' END x;
+------------+
| x          |
+------------+
| NOT FRIDAY |
+------------+

10-07 15:01