选择两个日期之间的日期范围并获得计数

时间:2016-08-31 06:46:57

标签: mysql sql date

我有一张桌子(名字vcar)

id  | createddatetime    | 
--------------------------
1   |2016-08-20 17:23:53 |
2   |2016-08-20 17:23:53 |
3   |2016-08-20 17:23:53 |
4   |2016-08-20 17:23:53 |
4   |2016-08-20 17:23:53 |
4   |2016-08-21 17:23:53 |
3   |2016-08-21 17:23:53 |
1   |2016-08-21 17:23:53 |
1   |2016-08-21 17:23:53 |
2   |2016-08-24 17:23:53 |
3   |2016-08-24 17:23:53 |
4   |2016-08-24 17:23:53 |
2   |2016-08-29 17:23:53 |
2   |2016-08-29 17:23:53 |
3   |2016-08-29 17:23:53 |
4   |2016-08-29 17:23:53 |
4   |2016-08-30 17:23:53 |
2   |2016-08-30 17:23:53 |
3   |2016-08-30 17:23:53 |
4   |2016-08-30 17:23:53 |

我尝试使用

获取特定日期范围中id的计数
SELECT
 count(CAST(createddatetime AS DATE)),
 CAST(createddatetime AS DATE),
 DAYNAME(CAST(createddatetime AS DATE)) 
FROM vcar 
WHERE
CAST(createddatetime AS DATE)>=date('2016-08-20') AND 
CAST(createddatetime AS DATE)<=date('2016-08-35') 
GROUP BY CAST(createddatetime AS DATE);

这给了我o / p,但我还需要显示在某一天没有点击的日期。如何做到这一点?

O / p我正在

-------------------------------------- ------------------------------- ----------------------------------------

     count(CAST(createddatetime AS DATE)) | CAST(createddatetime AS DATE) | DAYNAME(CAST(createddatetime AS DATE)) |

        -------------------------------------- ------------------------------- ----------------------------------------
                                            9 | 2016-08-20                    | Saturday                               |
                                           36 | 2016-08-23                    | Tuesday                                |
                                            4 | 2016-08-24                    | Wednesday                              |
        -------------------------------------- ------------------------------- ----------------------------------------

我需要什么

-------------------------------------- ------------------------------- ----------------------------------------
 count(CAST(createddatetime AS DATE)) | CAST(createddatetime AS DATE) | DAYNAME(CAST(createddatetime AS DATE)) |
-------------------------------------- ------------------------------- ----------------------------------------
                                    9 | 2016-08-20                    | Saturday                               |
                                    0 | 2016-08-21                    | Sunday                                 |
                                    0 | 2016-08-22                    | Monday                                 |
                                   36 | 2016-08-23                    | Tuesday                                |
                                    4 | 2016-08-24                    | Wednesday                              |
                                    9 | 2016-08-25                    | Thursday                               |
                                    0 | 2016-08-26                    | Friday                                 |                        
-------------------------------------- ------------------------------- ----------------------------------------

1 个答案:

答案 0 :(得分:2)

日历表会让这更容易。由于您没有,因此以下查询将完成此任务。

SELECT 
dateTable.day,
DAYNAME(dateTable.day) AS dayName,
COALESCE(yourQuery.total,0) AS cnt
FROM 
(
    SELECT ADDDATE('2016-08-20', INTERVAL @i:=@i+1 DAY) AS DAY
    FROM (
    SELECT a.a
    FROM (SELECT 0 AS a 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) AS a
    CROSS JOIN (SELECT 0 AS a 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) AS b
    CROSS JOIN (SELECT 0 AS a 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) AS c
    ) a
    JOIN (SELECT @i := -1) r1
    WHERE 
    @i < DATEDIFF('2016-08-31', '2016-08-20')
) AS dateTable
LEFT JOIN
(
    SELECT
        DATE(createddatetime) date,
        count(id) As total
    FROM vcar 
    WHERE DATE(createddatetime) BETWEEN '2016-08-20' AND '2016-08-31'
    GROUP BY date
) AS yourQuery
ON dateTable.day = yourQuery.date 
ORDER BY dateTable.day

注意:

dateTable别名中的查询将生成从2016-08-202016-08-31的日期。

Check this