Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何按外部值分组?mysql_Mysql_Sql_Group By - Fatal编程技术网

如何按外部值分组?mysql

如何按外部值分组?mysql,mysql,sql,group-by,Mysql,Sql,Group By,我要运行此查询: 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)

我要运行此查询:

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