我有下表:
product_ID | detail_ID
15228 | 1
19450 | 1
21309 | 336
21309 | 425
21310 | 336
26415 | 148
28842 | 497
53443 | 1
我目前正在运行以下查询:
SELECT product_ID, GROUP_CONCAT(DISTINCT detail_ID separator ', ') AS detailIds
FROM productdetails
GROUP BY product_ID
此查询为我提供了以下结果:
product_ID | detailIds
15228 | 1
19450 | 1
21309 | 336, 425
21310 | 336
26415 | 148
28842 | 497
53443 | 1
我想要回归的是:
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
我曾尝试在group_concat上使用group_concat和group_concat等聚合函数,但mysql对这些尝试非常不满意。任何建议,将不胜感激!
我试图在MySQL中这样做。
目标是找到具有相似功能的产品,然后查看这些类似功能的出现频率。谢谢!
答案 0 :(得分:1)
将您的查询用作子查询并执行另一个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;