【问题标题】:Postgres - select element from arrayPostgres - 从数组中选择元素
【发布时间】:2017-10-19 17:54:32
【问题描述】:

在我的表中,我有一个名为 facebook 的列作为文本 [] 类型。例如一行是:

{{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"

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

【问题讨论】:

    标签: php sql postgresql postgresql-9.3


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 2021-01-08
      • 1970-01-01
      • 2016-09-22
      • 2017-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-13
      • 1970-01-01
      相关资源
      最近更新 更多