Domanda

Io so come usare group_concat con SQLite, per effettuare le seguenti operazioni:

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 da tavolo gruppo da f1

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

come si può vedere, il 1 e 3 sono caduto ID, che è il comportamento previsto. Ma avrei bisogno di:

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

così, ogni record restituito, e un altro campo (f3) aggiornato con il group_concat

alcuna idea di come questo potrebbe essere fatto in Sqlite?

Grazie

È stato utile?

Soluzione

Non certo perché si desidera che questo, ma qui va:

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
;

Altri suggerimenti

utilizzare un'istruzione SQL incorporato

select id, f1, f2, (select group_concat(f2) from t t2 where t2.f1 = t1.f1)
from t t1
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top