在mysql中修改通过group_concat()得到的值
Modify the values obtained through group_concat() in mysql
我有以下 [table 个]
id result
1 a
1 b
1 b
1 c
2 e
2 e
2 e
2 f
我在执行 group_concat
后得到以下信息
select id , Group_Concat(result) from [table a]
group by id
id result
1 a,b,b,c
2 e,e,e,f
但是我想显示结果集中某个值出现在该值之前的次数,以避免像下面这样的冗余
id result
1 a,2 b,c
2 3 e,f
如何实现?
先按ID分组,再按结果分组,得到计数。然后按 ID 分组以构建您的字符串。
select
id,
group_concat(case when cnt = 1 then result else concat(cnt, ' ', result) end) as results
from
(
select id, result, count(*)
from mytable
group by id, result
) t
group by id;
我有以下 [table 个]
id result
1 a
1 b
1 b
1 c
2 e
2 e
2 e
2 f
我在执行 group_concat
后得到以下信息select id , Group_Concat(result) from [table a]
group by id
id result
1 a,b,b,c
2 e,e,e,f
但是我想显示结果集中某个值出现在该值之前的次数,以避免像下面这样的冗余
id result
1 a,2 b,c
2 3 e,f
如何实现?
先按ID分组,再按结果分组,得到计数。然后按 ID 分组以构建您的字符串。
select
id,
group_concat(case when cnt = 1 then result else concat(cnt, ' ', result) end) as results
from
(
select id, result, count(*)
from mytable
group by id, result
) t
group by id;