简体   繁体   中英

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. Any suggestions, would be greatly appreciated!

I am trying to do this in 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() :

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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