我知道如何在Sqlite中使用group_concat来执行以下操作:
id - f1 - f2 - f3
1 - 1 - a - NULL
2 - 1 - b - NULL
3 - 2 - c - NULL
4 - 2 - d - NULL
选择id,f1,group_concat(f2),f3
从表
按f1分组
result:
2 - 1 - a,b - NULL
4 - 2 - c,d - NULL
如您所见,ID的1和3被删除,这是预期的行为。
但我需要:
1 - 1 - a - a,b
2 - 1 - b - a,b
3 - 2 - c - c,d
4 - 2 - d - c,d
因此,返回的每条记录,以及使用group_concat更新的另一个字段(f3)
任何想法如何在Sqlite中完成?
谢谢
最佳答案
不确定为什么要这样做,但是这里有:
select
outer_t.id
,outer_t.f1
,outer_t.f2
,inline_view.groupfoo
from t as outer_t
left join (
select
f1
,group_concat(f2) as groupfoo
from t
group by f1
) inline_view on inline_view.f1 = outer_t.f1
;