id attr_id attr_value
1 2 a
2 2 b
3 2 b
4 1 a
5 1 a
6 1 cselect id ,attr_id,group_concat(distinct(ga.attr_value)) as attr_value from test group by attr_id能得到以下结果
3 2 a,b
6 1 a,c如果我想得到
3 2 a,b 1,3
6 1 a,c 5,6这样的结果。怎么实现呢。就是相当于同时 concat(",",id)
1 2 a
2 2 b
3 2 b
4 1 a
5 1 a
6 1 cselect id ,attr_id,group_concat(distinct(ga.attr_value)) as attr_value from test group by attr_id能得到以下结果
3 2 a,b
6 1 a,c如果我想得到
3 2 a,b 1,3
6 1 a,c 5,6这样的结果。怎么实现呢。就是相当于同时 concat(",",id)
group_concat(min(id),max(id)) from test group by attr_id
斑竹,可能我表达的意思有点问题。你这个SQL只是可以处理我这个特殊例子,如果是以下数据呢
id attr_id attr_value
1 2 a
2 2 b
3 2 b
7 2 e
4 1 a
5 1 a
6 1 c
我想得到以下结果
如果我想得到
3 2 a,b,e 1,3,7
6 1 a,c 5,6其实我就想获取group_concat(dinstict(attr_value))之后,同时返回被统计的attr_value对应的id
group_concat(min(id),max(id)
from test
group by attr_id;
id attr_id attr_value
1 2 a
2 2 b
3 2 b
7 2 e
4 1 a
5 1 a
6 1 c
我想得到以下结果
如果我想得到
2 a,b,e 1,3,7
1 a,c 5,6实我就想获取group_concat(dinstict(attr_value))之后,同时返回被统计的attr_value对应的id
select attr_id,group_concat(a.id),group_concat(attr_value) from tty a where not exists(
select 1 from tty where a.attr_id=attr_id and a.id<id and a.attr_value=attr_value)
group by attr_id