2015-12-03 4 views
2

Disons que j'ai deux tables:Joignez-vous à la suite de la fonction set-retour (json_array_elements) avec colonne de table

User_Combination

+--------+----------------+ 
| id | combination | 
+--------+----------------+ 
| 6 |  [1, 2]  | 
| 9 |  [2, 3]  | 
+--------+----------------+ 

Couleurs

+--------+----------------+ 
| id | color  | 
+--------+----------------+ 
| 1 |  Blue  | 
| 2 |  Yellow | 
| 3 |  Green | 
+--------+----------------+ 

Je suis en train de se joindre à la résultat de json_array_elements(color) avec le id d'éléments. Par exemple, le résultat de

select json_array_elements(color) as CombinationID 
from User_Combination where id = 6; 

est

+-------------------+ 
| CombinationID | 
+-------------------+ 
| 1    | 
| 2    | 
+-------------------+ 

Je suis incapable de rejoindre CombinationID avec Colors.id. Lorsque je tente une commande SQL comme:

select json_array_elements(article_data) AS articlesInOutfits (color) as CombinationID 
from User_Combination uc JOIN Colors co ON co.id = articlesInOutfits; 

ou

select json_array_elements(article_data) AS articlesInOutfits (color) as CombinationID 
from User_Combination uc JOIN Colors co ON co.id = uc.articlesInOutfits; 

Il dit articlesInOutfits n'existe pas.
Un conseil?

+1

Votre version de Postgres? –

+0

Utilisation de 9.3.3. Étonnamment, unnest ne fonctionne pas. – NateW

+0

Pouvez-vous corriger certaines erreurs confuses dans votre question? 'json_array_elements (couleur)'? Et à quoi se réfère 'Elements '? Les définitions de tables réelles montrant les types de données et les contraintes seraient beaucoup plus utiles. –

Répondre

3

Utilisez unnest() pour obtenir décompressé combinaisons:

select id, unnest(combination) cid 
from user_combination; 

id | cid 
----+----- 
    6 | 1 
    6 | 2 
    9 | 2 
    9 | 3 
(4 rows)  

Utilisez déballéescids à se joindre à colors:

select u.id, color 
from (
    select id, unnest(combination) cid 
    from user_combination 
    ) u 
join colors c 
on cid = c.id; 

id | color 
----+-------- 
    6 | Blue 
    6 | Yellow 
    9 | Yellow 
    9 | Green 
(4 rows) 

Utiliser une fonction d'agrégation (par exemple json_agg()) pour obtenir des couleurs jointes agrégées pour un utilisateur:

select u.id, json_agg(color) 
from (
    select id, unnest(combination) cid 
    from user_combination 
    ) u 
join colors c 
on cid = c.id 
group by 1; 

id |  json_agg  
----+--------------------- 
    9 | ["Yellow", "Green"] 
    6 | ["Blue", "Yellow"] 
(2 rows)  

Si combination est de type json vous devez utiliser json_array_elements() dans un latéral rejoindre:

select u.id, json_agg(color) 
from (
    select id, cid 
    from user_combination, 
    lateral json_array_elements(combination) cid 
    ) u 
join colors c 
on cid::text::int = c.id 
group by 1; 
+0

génial, une seconde et je vais essayer cela – NateW

+0

J'utilise Postgres version 9.3.3 et il dit: ERREUR: la fonction unnest (json) n'existe pas – NateW

+0

Ahh je vois, je stocke un tableau comme le type Json. Je vais corriger cela et essayer unnest – NateW