使用mysql查询在一个月内获取星期日的所有日子?

时间:2012-12-07 12:01:29

标签: mysql sql multithreading

例如。

本月是2012年12月,据我们所知,2012年这个月的所有星期日都是2,9,16,23,30。

任何人都可以告诉我如何在一个月内使用mysql查询获得如下所示的星期日的所有日子?

 _ _ _ _ _ _
|  Sundays  |
|- - - - - -| 
|    2      |
|    9      |
|    16     |
|    23     |
|    30     |
 - - - - - - 

1 个答案:

答案 0 :(得分:2)

Here is a SQLFiddle demo

select DATE_ADD('2012-12-01', INTERVAL ROW DAY) as Date,
row+1  as DayOfMonth from

(
SELECT @row := @row + 1 as row FROM 
(select 0 union all select 1 union all select 3 
        union all select 4 union all select 5 union all select 6) t1,
(select 0 union all select 1 union all select 3 
        union all select 4 union all select 5 union all select 6) t2, 
(SELECT @row:=-1) t3 limit 31
) b
where 
DATE_ADD('2012-12-01', INTERVAL ROW DAY)
between '2012-12-01' and '2012-12-31'
and
DAYOFWEEK(DATE_ADD('2012-12-01', INTERVAL ROW DAY))=1