MySQL活动每周但每个月只有第一周?

时间:2018-06-03 11:18:06

标签: mysql mysql-event

我需要每周(每个月的第一个星期五)举办活动,每个星期五除了当月的第一个星期五以外举办另一个活动。我能想到的只是

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

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

但是有更好/更整洁的查询吗?

1 个答案:

答案 0 :(得分:1)

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 |
+------------+