mysql事件每周但每月只有第一周?

7dl7o3gd  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(351)

我需要运行一个事件每周(每个月的第一个星期五)和另一个事件每周五以外的第一个星期五。我所能想到的就是

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

以及

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

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

8oomwypt

8oomwypt1#

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

相关问题