PostgreSQL JSON 数组中存在键
Existance of key in JSON array in PostgreSQL
我遇到了一个我不太明白的问题。
假设我有一个名为电影的列,看起来像这样 (jsonb):
[{"title": "Pulp Fiction"}, {"tiitle": "Rambo"}]
有没有办法找到 movies 具有 object 键标题的行?
对于 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 together with Postgres documentation.
我遇到了一个我不太明白的问题。
假设我有一个名为电影的列,看起来像这样 (jsonb):
[{"title": "Pulp Fiction"}, {"tiitle": "Rambo"}]
有没有办法找到 movies 具有 object 键标题的行?
对于 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 together with Postgres documentation.