【问题标题】:Existance of key in JSON array in PostgreSQLPostgreSQL 中 JSON 数组中存在键
【发布时间】:2015-11-24 09:30:02
【问题描述】:

我有一个我无法弄清楚的问题。

假设我有一个名为 movies 的列,看起来像这样 (jsonb):

[{"title": "Pulp Fiction"}, {"tiitle": "Rambo"}]

有没有办法找到movies 有一个带有键标题的对象的行?

【问题讨论】:

    标签: json postgresql psql


    【解决方案1】:

    对于 Postgres 9.4:

    ds=# CREATE TABLE yourtable (movie jsonb);
    CREATE TABLE
    ds=# \d+ yourtable 
                          Table "public.yourtable"
     Column | Type  | Modifiers | Storage  | Stats target | Description 
    --------+-------+-----------+----------+--------------+-------------
     movie  | jsonb |           | extended |              | 
    
    ds=# INSERT INTO yourtable VALUES ('[{"title": "Pulp Fiction"}, {"tiitle": "Rambo"}]');
    INSERT 0 1
    
    ds=# SELECT * FROM yourtable yt,jsonb_array_elements(yt.movies) AS movie
    WHERE movie ? 'tiitle';
    

    SO 有很多 good resources 以及 Postgres documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-17
      • 1970-01-01
      • 2020-09-22
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      • 2017-06-22
      相关资源
      最近更新 更多