仅当 id 不同时才在 group_concat 中获取重复值

Eld*_*ros 3 mysql select group-concatenation

我有两个表,首先是表 Product:

id|category_id
--+-----------
1 | 12345
2 | 12345
3 | 12465
Run Code Online (Sandbox Code Playgroud)

然后是表活动:

id|prod_id|activity_type   |description
--+-------+----------------+-----------
1 | 1     | Initialization | blah
2 | 1     | Finalization   | foo
3 | 2     | Initialization | blah again
4 | 2     | Duplication    | bar
5 | 2     | Finalization   | foobar
6 | 3     | Initialization | blob
7 | 3     | Migration      | A to B
8 | 3     | Migration      | B to C
9 | 3     | Finalization   | fuh
Run Code Online (Sandbox Code Playgroud)

现在我想为每种类型的活动检索至少具有一种此类活动的产品数量,以及产品类别列表。类别将在此类别的每个产品的列表中重复。现在我正在使用以下查询:

SELECT a.activity_type as Activity, COUNT(DISTINCT p.id) as Products,
CONVERT(GROUP_CONCAT(p.category SEPARATOR ',  ') USING utf8) AS Categories
FROM mydb.product p, mydb.activity a
WHERE p.id = a.prod_id
AND a.activity_type <> '' // To not count activities which haven't been correctly initialized
GROUP BY Categories
ORDER BY Products
Run Code Online (Sandbox Code Playgroud)

现在我等待的结果是:

Activity       | Products | Categories
---------------+----------+--------------------
Initialization | 3        | 12345, 12345, 12465
Finalization   | 3        | 12345, 12345, 12465
Duplication    | 1        | 12345
Migration      | 1        | 12465
Run Code Online (Sandbox Code Playgroud)

但是通过这个查询,我得到了'12465, 12465'迁移的价值。我可以得到一个类别出现在列表中,仅针对每个不同的产品 ID,而不是针对一种类型的每个活动?

ype*_*eᵀᴹ 5

双方第一组activity_typeprod_id,然后另一组由activity_type

SELECT 
    a.activity_type AS Activity, 
    COUNT(DISTINCT p.id) AS Products,
    CONVERT(GROUP_CONCAT(p.category_id SEPARATOR ',  ') USING utf8) 
      AS Categories
FROM 
    product AS p
  JOIN 
    ( SELECT activity_type
           , prod_id
      FROM activity 
      WHERE activity_type <> '' 
      GROUP BY activity_type
             , prod_id
    ) AS a
    ON p.id = a.prod_id
GROUP BY 
    activity_type
ORDER BY 
    Products DESC;
Run Code Online (Sandbox Code Playgroud)

SQL-Fiddle 中测试(谢谢@Mr.Radical)

您也可以安全地替换上面的COUNT(DISTINCT p.id)with COUNT(*),对于每个活动类型,只有不同的产品 ID(这在内部 group by 中进行了处理)。