2017-10-19 179 views
1

在我的表中我有一個名爲facebook的列作爲text []類型。例如,一行是:Postgres - 從數組中選擇元素

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

現在我想只SELECT total_count。我一直在試圖對一切,結束本:

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)