[英]Counting patterns from table mysql
我在Mysql表中有以下数据
ID | code | code order
1 | 1 | 1
1 | 2 | 2
1 | 3 | 3
2 | 1 | 1
2 | 2 | 2
2 | 3 | 3
3 | 1 | 1
3 | 4 | 2
3 | 5 | 3
4 | 1 | 1
4 | 4 | 2
4 | 5 | 3
4 | 6 | 4
我如何编写查询以返回以下结果
code pattern 1,2,3 = 2 (count)
code pattern 1,4,5 = 1 (count)
code pattern 1,4,5,6 = 1 (count)
基本上,我需要找出最受欢迎的代码序列,每个序列按唯一的ID分组。 的代码顺序也很重要。 即
1,4,5,6不同于1,5,4,6
干杯
在MySQL中,使用两个聚合最容易做到这一点:
select pattern, count(*)
from (select id, group_concat(code order by code) as pattern
from t
group by id
) p
group by pattern;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.