简体   繁体   English

日期范围内每天的棘手mysql计数

[英]Tricky mysql count occurrences of each day within date range

I'm having trouble with getting the records for the following 我在获取以下记录时遇到麻烦

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

I'm trying to get occurrences of holidays for a given month - ie 我正在尝试获取给定月份的假期-例如

BETWEEN "2012-01-01" AND "2012-01-31"

there is a similar post but im unable to tweak it for my needs 有类似的帖子,但我无法根据我的需要进行调整

RESULT
day (within range) | count() //number of ppl on holiday
DATE               | 3

for eg 例如

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

In other words I am trying to get how many times a record is found for a specific date. 换句话说,我正在尝试获取在特定日期找到记录的次数。 Ie how many people are on holiday on the 1st, 2nd 3nd etc. 即1号,2号,3号等有多少人在度假。

Not every day is in the TABLE for each month 并非每个月的每一天都在表格中

Any ideas? 有任何想法吗?

ps this is what i have already (my shot in the dark) 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

Please, do not be scared :)) 拜托,不要害怕:))

Based on generate days from date range 基于日期范围内的生成天数

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

Result: 结果:

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

ps: I renamed columns to hFrom and hTo ps:我将列重命名为hFrom和hTo

pps: updated variant for the range of dates 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

Updated - number 2 was missing from all the unions. 已更新 -所有工会都缺少2号。 it should not significantly affect anything. 它不会对任何事物产生重大影响。

Have you tried something like this? 你尝试过这样的事情吗?

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_last-day http://dev.mysql.com/doc/refman/5.5/zh-CN/date-and-time-functions.html#function_last-day

This function will give you the last day of a month regardless of the variables involved like month/leapyear/etc... 此功能将为您提供每月的最后一天,而不管涉及的变量如month / leapyear / etc...。

That might be the problem you were having. 那可能是您遇到的问题。 It's not exactly clear... 还不清楚...

SELECT h.holiday_From, h.holiday_To, COUNT( * )
FROM holiday h
JOIN holiday ho 
    ON h.holiday_From BETWEEN DATE( "2012-01-01" ) AND LAST_DAY("2012-01-01" )
GROUP BY h.holiday_From, h.holiday_To

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM