MySQL在工作日加入表格+时间戳小时,每个表计数

时间:2013-10-19 13:33:43

标签: mysql datetime join count timestamp

我有几张具有完全相同结构的表格。它们包含来自年度活动入口处填写的表格的数据。这些表中的一个字段是DATETIME,其中包含条目的日期和时间。

出于统计目的,我试图比较每年每小时的参赛作品数量。当然,我可以为每个表运行单独的查询,并将它们放在PHP中。但我认为在单一查询中也应该是可能的。但是,我无法弄清楚如何建立正确的查询。

这是我到目前为止所得到的:

SELECT
    WEEKDAY(P3.AddedOn) Day,
    HOUR(P3.AddedOn) Hour,
    COUNT(P3.AddedOn) Entries2013,
    COUNT(P2.AddedOn) Entries2012
FROM Event2013 P3
LEFT JOIN Event2012 P2
    ON WEEKDAY(P3.AddedOn) = WEEKDAY(P2.AddedOn)
        AND HOUR(P3.AddedOn) = HOUR(P2.AddedOn)
GROUP BY WEEKDAY(P3.AddedOn), WEEKDAY(P2.AddedOn), HOUR(P3.AddedOn), HOUR(P2.AddedOn)

但是这个查询会产生一些奇怪的结果,包括过多的数字和Entries2012和Entries2013列中的相同数字。它增加了一些数据,但我无法确切地知道哪些数据。

我做错了什么?

感谢您的帮助!

-

用子查询解决:

SELECT
    P2.Day,
    P2.Hour,
    P2.Entries Entries2012,
    P3.Entries Entries2013
FROM
    (
        SELECT
            WEEKDAY(AddedOn) Day,
            HOUR(AddedOn) Hour,
            COUNT(1) AS Entries
        FROM Event2012
        GROUP BY Day, Hour
    ) P2
LEFT JOIN
    (
        SELECT
            WEEKDAY(AddedOn) Day,
            HOUR(AddedOn) Hour,
            COUNT(1) AS Entries
        FROM Event2013
        GROUP BY Day, Hour
    ) P3
        ON P2.Day = P3.Day
            AND P2.Hour = P3.Hour

-

现在还有一些MySQL FULL OUTER JOIN:

SELECT
    Day,
    Hour,
    Entries2012,
    Entries2013
FROM (
    SELECT
        (CASE P2.Day
            WHEN 4 THEN "Fr"
            WHEN 5 THEN "Sat"
            WHEN 6 THEN "Sun"
            ELSE P2.Day
            END) AS Day,
        P2.Hour,
        COALESCE(P2.Entries, 0) Entries2012,
        COALESCE(P3.Entries, 0) Entries2013
    FROM (
        SELECT
            WEEKDAY(AddedOn) Day,
            HOUR(AddedOn) Hour,
            COUNT(1) AS Entries
        FROM Event2012
        GROUP BY Day, Hour) P2
    LEFT JOIN (
        SELECT
            WEEKDAY(AddedOn) Day,
            HOUR(AddedOn) Hour,
            COUNT(1) AS Entries
        FROM Event2013
        GROUP BY Day, Hour) P3
    ON P2.Day = P3.Day
        AND P2.Hour = P3.Hour
    UNION SELECT
        (CASE P3.Day
            WHEN 4 THEN "Fr"
            WHEN 5 THEN "Sat"
            WHEN 6 THEN "Sun"
            ELSE P3.Day
            END) AS Day,
        P3.Hour,
        COALESCE(P2.Entries, 0) Entries2012,
        COALESCE(P3.Entries, 0) Entries2013
    FROM (
        SELECT
            WEEKDAY(AddedOn) Day,
            HOUR(AddedOn) Hour,
            COUNT(1) AS Entries
        FROM Event2012
        GROUP BY Day, Hour) P2
    RIGHT JOIN (
        SELECT
            WEEKDAY(AddedOn) Day,
            HOUR(AddedOn) Hour,
            COUNT(1) AS Entries
        FROM Event2013
        GROUP BY Day, Hour) P3
    ON P2.Day = P3.Day
        AND P2.Hour = P3.Hour
    WHERE P2.Hour IS NULL) AS tmp
ORDER BY Day, Hour

2 个答案:

答案 0 :(得分:0)

LEFT JOIN表示您打算从P3获取所有条目,并期望其中一些条目在P2中不匹配。如果P3是您的基表(并且您在另一个表中查找可能的匹配项),则只需按P3列分组。

SELECT P3.Day Day, P3.Hour Hour, Entries2013, Entries2012 
FROM (
SELECT   
    WEEKDAY(AddedOn) Day,
    HOUR(AddedOn) Hour,
    COUNT(AddedOn) Entries2013
    GROUP BY Day, Hour
FROM Event2013) P3
LEFT JOIN (
SELECT 
    WEEKDAY(AddedOn) Day,
    HOUR(AddedOn) Hour,
    COUNT(AddedOn) Entries2012
    GROUP BY Day, Hour
FROM Event2012) P2
ON P3.DAY = P2.DAY
AND P3.Hour = P2.Hour
GROUP BY P3.Day, P3.Hour;

此外,使用WEEKDAY意味着您在给定年份中对所有星期一,星期二等进行求和,然后从中获得每小时摘要。如果您想获得每日摘要(当天是一年中的某一天),您应该使用DAYOFYEAR i / o WEEKDAY。

答案 1 :(得分:0)

查看UNION声明:http://dev.mysql.com/doc/refman/5.0/en/union.html

这结合了两个表的查询。