如何获取每个组concat字段的总数?

时间:2015-06-12 09:34:07

标签: mysql

我有一个名为'activity_data'

的表

表包含以下字段

id int(11),
sports_name varchar(20),
activity_date datetime

activity_data

的信息
id         sports_name      activity_date
1          Cricket          2015-05-12
2          Football         2015-05-12 
3          Cricket          2015-06-10    
4          Basketball       2015-06-08  
5          Khokho           2015-06-02 
6          Kubbdi           2015-06-04
7          Cricket          2015-05-12
8          Kubbdi           2015-05-12  
9          Cricket          2015-06-09   
10         Football         2015-06-03

现在我执行以下查询

SELECT GROUP_CONCAT(id) id ,GROUP_CONCAT(DISTINCT(sports_name)) sports_name,activity_date FROM filter_activity_data WHERE activity_date BETWEEN '2015-05-12' AND '2015-06-04' GROUP BY activity_date ORDER BY activity_date ASC

执行此操作时:输出结果如下

id         sports_name                activity_date
1,2,7,8    Cricket,Football,Kubbdi    2015-05-12
5          Khokho                     2015-06-02 
10         Football                   2015-06-03    
6          Kubbdi                     2015-06-04  

我想得到每个GROUP_CONCAT sports_name的输出总数。预计会这样:

id         sports_name                activity_date
2,1,1      Cricket,Football,Kubbdi    2015-05-12
1          Khokho                     2015-06-02 
1          Football                   2015-06-03    
1          Kubbdi                     2015-06-04  

如何实现这一目标?

1 个答案:

答案 0 :(得分:3)

嘿使用了以下查询,它可能会帮助你

select GROUP_CONCAT(t.name1) as name2 , GROUP_CONCAT(t.count) as counts , t.activity_date from 
(SELECT GROUP_CONCAT(id) id ,GROUP_CONCAT(DISTINCT(sports_name)) as name1 , count(*) as count , sports_name,activity_date FROM activity_data WHERE activity_date BETWEEN '2015-05-12' AND '2015-06-04' GROUP BY activity_date, sports_name ORDER BY activity_date ASC) as t 
group by t.activity_date

此查询可为您提供准确的结果。