Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.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_Database - Fatal编程技术网

从日期数据开始每年MYSQL计数

从日期数据开始每年MYSQL计数,mysql,sql,database,Mysql,Sql,Database,我需要问一些关于mysql中的多重计数的问题,所以我有这样的数据: | name | date | act | ------------------------------------------------ | abc | 2014-02-03 07:05:18 | 1 | | abc | 2014-02-03 08:05:18 | 1 | | abc | 2015-02-

我需要问一些关于mysql中的多重计数的问题,所以我有这样的数据:

|  name  |  date                        | act  |
------------------------------------------------
| abc    |  2014-02-03 07:05:18         |  1   |
| abc    |  2014-02-03 08:05:18         |  1   |
| abc    |  2015-02-03 07:05:18         |  1   |
| ghi    |  2014-02-03 07:05:18         |  1   |
| ghi    |  2015-02-03 07:05:18         |  1   |
| klm    |  2014-02-03 07:05:18         |  1   |
| klm    |  2014-04-01 07:05:18         |  1   |
然后我想做一个这样的报告:

|  name  |  count(2014)  |  count(2015)  |
------------------------------------------------
| abc    |       2       |      1        |
| ghi    |       1       |      1        |
| klm    |       2       |      0        |

如何进行计数查询?

如果年份是这两个年份,或者是有限的一组年份,您可以通过有条件计数来完成

select  name,
        sum(case when year(date) = 2014 then 1 else 0 end) as count_14,
        sum(case when year(date) = 2015 then 1 else 0 end) as count_15
from    yourTable
group by name
否则,我建议将年份移动到行,然后在表示层将其更改为列(或使用轴)


日期格式是表中数据的时间戳,如果您对静态解决方案感到满意,那么我们可以使用
CASE
select  name, year(date), count(*)
from    yourTable
group by name, year(date)