简体   繁体   English

SQLite group_concat选择“特殊需要”

[英]Sqlite group_concat select with “special needs”

I know how to use group_concat with Sqlite, to do the following: 我知道如何在Sqlite中使用group_concat来执行以下操作:

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

select id, f1, group_concat(f2), f3 from table group by f1 从表组中按f1选择id,f1,group_concat(f2),f3

 result:
 2 -  1 - a,b - NULL
 4 -  2 - c,d - NULL

as you can see, the ID's 1 and 3 are dropped, which is the expected behaviour. 如您所见,ID的1和3被删除,这是预期的行为。 But I would need: 但我需要:

 1 -  1 -  a - a,b
 2 -  1 -  b - a,b
 3 -  2 -  c - c,d
 4 -  2 -  d - c,d

so, every record returned, and another field (f3) updated with the group_concat 因此,返回了每个记录,并使用group_concat更新了另一个字段(f3)

any idea how this could be done in Sqlite? 任何想法如何在Sqlite中完成?

Thank you 谢谢

use an embedded sql statement 使用嵌入式sql语句

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

Not sure WHY you want this, but here goes: 不确定为什么要这样做,但是这里有:

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