2017-10-19 6 views
1

私のテーブルには、facebookという名前のカラムがtext []としてあります。例えば、1つの行がある:Postgres - 配列から要素を選択する

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

今、私は唯一のTOTAL_COUNTを選択しようとしています。私はeverythinこれで終了しようとしてきた:

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

をしかし、私はエラーを取得しています: 演算子は存在しません:記録 - >>不明\ n行1:

任意のアイデア?

//編集

今、私はこの

$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"; 

は私が注文するTOTAL_COUNTを追加することができる方法はあります持っていますか?

答えて

1

例えば、あなたがFacebookの属性にテキスト配列の配列を持っているので、to_jsonをあまりにも多次元配列に変換し、これ使用すると、配列を操作する必要があります。

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) 
関連する問題