簡體   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