sqlite group_concat选择“特殊需求”
-
19-09-2019 - |
题
我知道如何将group_concat与sqlite一起执行以下操作:
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),F1的F3 by 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
因此,返回的每个记录,另一个字段(F3)使用group_concat进行了更新
知道这如何在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
;
其他提示
使用嵌入式SQL语句
select id, f1, f2, (select group_concat(f2) from t t2 where t2.f1 = t1.f1)
from t t1
不隶属于 StackOverflow