我有这样的表(值列值分隔为“@#@”)
Id | Values
1 | abc@#@def@#@ghi@#@
2 | def@#@ghi@#@xyz@#@
3 | abc@#@def@#@xyz@#@
现在我希望结果计数为
abc = 2
def = 3
xyz = 2
我该怎么做?
最佳答案
如果您知道要查找的子字符串值,那就相当容易了。
select string,
(
length(group_concat(values)) -- length of whole thing
-length(group_concat(replace(values, string, '')) -- length of whole thing without searched string
)-- substraction equals length of string*count of strings
/length(string) -- length of string
as string_count
from values
cross join
(select abc as string
union all select def ...)
group by 1
您可能还需要扩展您的最大组连接
SET SESSION group_concat_max_len = 1000000;
如果您没有这些值,我建议您查看“mysqlexplode”,Can you split/explode a field in a MySQL query? 并预先创建一个包含这些值(不同)的表
关于mysql - 对 MySql 数组行进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32580226/