sql 某个字段中 包含字段最多","最多的 分组查询

来源:百度知道 编辑:UC知道 时间:2024/05/08 08:47:24
想要 每个 form分类中 type字段中逗号最多的 那条纪录

id type form
1 x,x a
2 x,x,x a
3 x b
4 x,x,x,x a
5 x,x,x b
6 x,x c
想要结果

id form ","的个数
4 a 3
5 b 2
6 c 1

再问问mysql能否实现 谢谢了

给你个MS SQL SERVER的方法,别的只要找到求某字串内函子串个数的函数替换就行了。
select a.id,a.form,b.个数 from tablename a,
(select form,个数=max(len(type)-len(replace(type,',',''))) from tablename group by type) b
where a.form=b.form and len(a.type)-len(replace(a.type,',',''))=len(b.type)-len(replace(b.type,',',''))

为什么要实现这个,目的是什么,看看是否有其他解决办法,现在只有问题,没有逻辑关系,很难理解。

也很难解答。

我觉得这个问题还是放在前台解决吧
如果是后台解决的话
我也不知道mysql 支持不支持存储过程的循环吧
如果支持 就可以解决
给你个思路 代码太多 我就不写出来了
while(id=1 where 结束条件 len(字段))
being
while(substring(index(组合使用)))
begin
end
end

select a.id,a.form,len(a.[type])-len(replace(a.[type],',','')) as [","的个数]
from table0 a inner join
(select max(len([type])-len(replace([type],',',''))) as cnt,form from table0
group by form) b on a.form = b.form and
len(a.[type])-len(replace(a.[type],',','')) = b.cnt