简体   繁体   English

MYSQL-来自group_concat的类似结果计数

[英]MYSQL - Count of similar results from a group_concat

I have the following table: 我有下表:

product_ID | detail_ID
15228 | 1
19450 | 1
21309 | 336
21309 | 425
21310 | 336
26415 | 148
28842 | 497
53443 | 1

And I am currently running the following query: 我目前正在运行以下查询:

SELECT product_ID, GROUP_CONCAT(DISTINCT detail_ID separator ', ') AS detailIds
FROM productdetails
GROUP BY product_ID

this query gives me the following results: 该查询给我以下结果:

product_ID | detailIds
15228      | 1
19450      | 1
21309      | 336, 425
21310      | 336
26415      | 148
28842      | 497
53443      | 1

What I would like to return is: 我想返回的是:

group_concat on product_ID | group_concat | count of group_concat
15228, 19450, 53443        | 1            | 3
21309                      | 336, 425     | 1
21310                      | 336          | 1
26415                      | 148          | 1
28842                      | 497          | 1

I have tried to use aggregate functions like count on the group_concat and group_concat on group_concat but mysql is very unhappy with these attempts. 我曾尝试在group_concat上使用count和group_concat上的group_concat等聚合函数,但是mysql对这些尝试非常不满意。 Any suggestions, would be greatly appreciated! 任何建议将不胜感激!

I am trying to do this in MySQL. 我正在尝试在MySQL中执行此操作。

The goal is to find products with similar features and then to see how often these similar features are appearing. 目的是找到具有相似功能的产品,然后查看这些相似功能出现的频率。 Thanks! 谢谢!

Use your query as a subquery and do another group_concat() : 将您的查询用作子查询并执行另一个group_concat()

SELECT GROUP_CONCAT(product_ID) as Products, detailIds, count(*) as NumProducts
FROM (SELECT product_ID, GROUP_CONCAT(DISTINCT detail_ID separator ', ') AS detailIds
      FROM productdetails
      GROUP BY product_ID
     ) P
GROUP BY detailIds;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM