mysql - 对 MySql 数组行进行分组

标签 mysql sql arrays count

我有这样的表(值列值分隔为“@#@”)

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/

相关文章:

mysql - 如何写Nhibernate Query C# .net Mysql

c# - 插入前检查值是否存在

python - 程序将数组分成N个连续的子数组,使得每个子数组的和为奇数

Javascript数组将每个值存储在变量中

sql - 我们怎样才能构建如此极其复杂的SQL语句呢?

javascript - 如何将搜索结果显示为 "Showing 1-10 of 300"

python - 在 Windows 8 中从 .sql 文件创建 sqlite3 数据库

php - 如何在普通的sql查询中设置限制

php - 从多个表中获取数据的最佳方式

php - 使用 PHP 将数组插入 MySQL