Postgres - 从数组

时间:2017-10-19 17:54:33

标签: php sql postgresql postgresql-9.3

在我的表中,我有一个名为facebook的列为text text []。例如,一行是:

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

现在我正在尝试仅选择total_count。我一直在努力,并以此结束:

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

但我收到一个错误: 运算符不存在:record - >>未知\ nLINE 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添加到ORDER?

1 个答案:

答案 0 :(得分: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)