0

我有一张桌子:

idint(11) NOT NULL
type varchar(64) NOT NULL
created_on datetime NOT NULL

使用doctrine2时,我可以在一个查询中按周计算所有类型的设备(iSO、Android ...)吗?

样本输出:

Week iOS Android Other
1     4    5       6
2     3    5       9
4

1 回答 1

1

这是完全可能的,使用类似于一年中某些列的输出总和中详述的方法,以周为间隔,周日期与日期一致,但在您描述的输出中并不容易。处理此问题的最佳方法是执行 SELECT,按 WEEK 和 TYPE 分组。IE

SELECT

    -- Get the week and month of each row
    YEAR(created_on) AS Year,
    WEEK(created_on) AS Week,
    type

    -- Count how many rows are in this group
    COUNT(*) AS frequency

FROM yourTable AS yt

-- Order by month and then week, so that week 4 in Jan comes before week 4 in Feb
GROUP BY
    Year ASC,
    Week ASC,
    type ASC

这将给出输出,例如

Year    Week    Type       frequency
2013    1       'iOS'      4
2013    1       'Android'  5
2013    1       'Other'    6
2013    2       'iOS'      3
2013    2       'Android'  5
2013    2       'Other'    9
于 2013-04-02T12:18:46.320 回答