繁体   English   中英

SQLite group_concat选择“特殊需要”

[英]Sqlite group_concat select with “special needs”

我知道如何在Sqlite中使用group_concat来执行以下操作:

id - f1 - f2 - f3
 1 -  1 -  a - NULL
 2 -  1 -  b - NULL
 3 -  2 -  c - NULL
 4 -  2 -  d - NULL

从表组中按f1选择id,f1,group_concat(f2),f3

 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中完成?

谢谢

使用嵌入式sql语句

select id, f1, f2, (select group_concat(f2) from t t2 where t2.f1 = t1.f1)
from t t1

不确定为什么要这样做,但是这里有:

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
;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM