我想运行此查询:

SELECT count(statistics.user_id) as y, YEAR('created_time') as t
WHERE   ....
GROUP(YEAR('created_time'))


查询运行完美!但是问题是多年来我的数据库都没有返回行。
我希望查询返回空行,这些年不包含在数据库中!例如从1991年到2015年?

怎么做 ?

最佳答案

您可以将“ DateTime”表与所需数据一起使用,或将其构建为子查询:

SELECT  count(s.user_id) as y
        ,t.yr
FROM    statistics s
RIGHT OUTER
JOIN    (
                select 1991 as yr
        union   select 1992
        ...
        union   select 2014
        union   select 2015
        ) t
    on  YEAR(s.created_time) = t.yr
WHERE   ....
GROUP BY t.yr

关于mysql - 如何按外部值(value)分组? MySQL的,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28715373/

10-11 03:31
查看更多