棘手的mysql计算日期范围内每天的出现次数

Ste*_*cus 1 php mysql date

我在获取以下记录时遇到问题

TABLE
id | holiday_From | holiday_To
1  | 2012-01-02   | 2012-01-03
1  | 2012-01-11   | 2012-01-16
1  | 2012-01-08   | 2012-01-22
1  | 2012-01-29   | 2012-01-30
1  | 2012-01-08   | 2012-01-11
Run Code Online (Sandbox Code Playgroud)

我正在尝试获取给定月份的假期 - 即

BETWEEN "2012-01-01" AND "2012-01-31"
Run Code Online (Sandbox Code Playgroud)

有一个类似的帖子,但我无法根据我的需要调整它

RESULT
day (within range) | count() //number of ppl on holiday
DATE               | 3
Run Code Online (Sandbox Code Playgroud)

例如

SAMPLE OUTPUT
2012-01-02 | 1
2012-01-03 | 1
2012-01-08 | 2
2012-01-09 | 2
2012-01-10 | 2
2012-01-11 | 3
2012-01-12 | 2
2012-01-13 | 2
2012-01-14 | 2
2012-01-15 | 2
2012-01-16 | 2
......
Run Code Online (Sandbox Code Playgroud)

换句话说,我试图获取在特定日期找到记录的次数。即1号、2号、3号等有多少人在度假。

不是每个月的每一天都在表格中

有任何想法吗?

ps 这就是我已经拥有的(我在黑暗中拍摄的)

SELECT h.holiday_From, h.holiday_To, COUNT( * )
FROM holiday h
JOIN holiday ho ON h.holiday_From
BETWEEN DATE( "2012-01-01" )
AND IF( DATE( "2012-01-31" ) , DATE( "2012-01-31" ) , DATE( "2012-01-01" ) )
GROUP BY h.holiday_From, h.holiday_To
Run Code Online (Sandbox Code Playgroud)

Che*_*ery 6

请不要害怕:))

基于从日期范围生成天数

select d.everyday, count(*) from (select @rownum:=@rownum+1, date('2012-01-01') + interval (@rownum-1) day everyday from
(select 0 union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t,
(select 0 union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t2,
(select 0 union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t3,
(SELECT @rownum:=0) r WHERE @rownum < DAY(LAST_DAY('2012-01-01'))) d, tablename tbl WHERE d.everyday>=tbl.hFrom AND d.everyday<tbl.hTo GROUP BY d.everyday
Run Code Online (Sandbox Code Playgroud)

结果:

2012-01-02  1
2012-01-08  2
2012-01-09  2
2012-01-10  2
2012-01-11  2
2012-01-12  2
2012-01-13  2
2012-01-14  2
2012-01-15  2
2012-01-16  1
2012-01-17  1
2012-01-18  1
2012-01-19  1
2012-01-20  1
2012-01-21  1
2012-01-29  1
Run Code Online (Sandbox Code Playgroud)

ps:我将列重命名为 hFrom 和 hTo

pps:更新日期范围的变体

select d.everyday, count(*) from (select @rownum:=@rownum+1, date('2012-01-01') + interval (@rownum - 1) day everyday from
(select 0 union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t,
(select 0 union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t2,
(select 0 union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t3,
(SELECT @rownum:=0) r WHERE @rownum <= DATEDIFF('2012-01-11','2012-01-01')) d, `test` tbl WHERE d.everyday BETWEEN tbl.hFrom AND tbl.hTo GROUP BY d.everyday
Run Code Online (Sandbox Code Playgroud)

更新- 所有工会都缺少 2 号。它不应该对任何事情产生重大影响。