SQLITE GROUP_CONCAT SELECT avec "Besoins spéciaux"
-
19-09-2019 - |
Question
Je sais comment utiliser group_concat avec sqlite, pour faire ce qui suit:
id - f1 - f2 - f3
1 - 1 - a - NULL
2 - 1 - b - NULL
3 - 2 - c - NULL
4 - 2 - d - NULL
Sélectionnez ID, F1, groupe_concat (F2), F3 dans le groupe de table par F1
result:
2 - 1 - a,b - NULL
4 - 2 - c,d - NULL
Comme vous pouvez le voir, les ID 1 et 3 sont abandonnés, ce qui est le comportement attendu. Mais j'aurais besoin:
1 - 1 - a - a,b
2 - 1 - b - a,b
3 - 2 - c - c,d
4 - 2 - d - c,d
Ainsi, chaque enregistrement est retourné et un autre champ (F3) mis à jour avec le groupe_concat
Une idée de comment cela pourrait être fait dans SQLite?
Merci
La solution
Je ne sais pas pourquoi vous voulez cela, mais voilà:
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
;
Autres conseils
Utilisez une instruction SQL intégrée
select id, f1, f2, (select group_concat(f2) from t t2 where t2.f1 = t1.f1)
from t t1
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow