2017-10-19 5 views
1

Dans mon tableau, j'ai une colonne nommée facebook comme type text []. Par exemple, une ligne est:Postgres - sélection d'un élément du tableau

{{total_count,26861},{comment_count,94},{comment_plugin_count,0},{share_count,26631},{reaction_count,136}} 

Maintenant, j'essaie de sélectionner uniquement total_count. J'ai essayé everythin et se termine par ceci:

SELECT json_each(to_json(facebook))->>'total_count' AS total_count" 

Mais je reçois une erreur: opérateur n'existe pas: enregistrement - >> inconnu \ nLigne 1:

Toutes les idées?

// modifier

Maintenant, j'ai ce

$select = "WITH fejs AS (select json_array_elements(to_json(facebook)) e FROM $table), arr AS (select e->1 as total_count FROM fejs WHERE e->>0 = 'total_count') SELECT ".implode(", ", SSP::pluckas($columns))." 
     , count(*) OVER() AS full_count 
     FROM $table    
     $where 
     $order 
     $limit"; 

Est-il possible que je peux ajouter TOTAL_COUNT COMMANDER?

Répondre

1

vous tableau de tableaux de texte dans attribut facebook, donc to_json convertit en tableau multidimensionnel aussi, donc vous devez utiliser des tableaux, par exemple:

t=# with t(facebook) as (values('{{total_count,26861},{comment_count,94},{comment_plugin_count,0},{share_count,26631},{reaction_count,136}}'::text[])) 
, arr as (select json_array_elements(to_json(facebook)) e from t) 
select e->1 as total_count from arr where e->>0 = 'total_count'; 
total_count 
------------- 
"26861" 
(1 row)