Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql Postgres选择包含json的json数组中的位置_Sql_Json_Postgresql_Postgresql 9.4_Jsonb - Fatal编程技术网

Sql Postgres选择包含json的json数组中的位置

Sql Postgres选择包含json的json数组中的位置,sql,json,postgresql,postgresql-9.4,jsonb,Sql,Json,Postgresql,Postgresql 9.4,Jsonb,我在postgresql 9.4数据库中有一个带有jsonb字段的表。 一些示例行: [{"name": "145119603", "numberOfCarrot": 2}] [{"name": "1884595530", "numberOfCarrot": 1}] [{"name": "363058213", "numberOfCarrot": 1}] [{"name": "1427965764", "numberOfCarrot": 1}] [{"name": "193623800", "

我在postgresql 9.4数据库中有一个带有jsonb字段的表。 一些示例行:

[{"name": "145119603", "numberOfCarrot": 2}]
[{"name": "1884595530", "numberOfCarrot": 1}]
[{"name": "363058213", "numberOfCarrot": 1}] 
[{"name": "1427965764", "numberOfCarrot": 1}]
[{"name": "193623800", "numberOfCarrot": 43}, {"name": "419955814", "numberOfCarrot": 0}]
[{"name": "624635532", "numberOfCarrot": 0}, {"name": "1884595530", "numberOfCarrot": 1}]
[{"name": "791712670", "numberOfCarrot": 1}]
[{"name": "895207852", "numberOfCarrot": 0}]
[{"name": "144695994", "numberOfCarrot": 3}, {"name": "384217055", "numberOfCarrot": 23}]
[{"name": "1079725696", "numberOfCarrot": 10}]
  • 我怎样才能找到所有行的最大胡萝卜数
  • 我怎样才能找到一排胡萝卜的最大数量
  • 我怎样才能找到胡萝卜数优于或低于某物的那一行
  • 如何在json数组中找到numberOfCarrot优于或低于某个元素的元素
您需要一个唯一的列(通常是主键,比如说
id
),因此您的数据应该如下所示:

(1, '[{"name": "145119603", "numberOfCarrot": 2}]'),
(2, '[{"name": "1884595530", "numberOfCarrot": 1}]'),
...
在横向连接中使用
jsonb\u array\u elements()。示例:

select max(value->>'numberOfCarrot')::int
from my_table,
jsonb_array_elements(jdata);

 max 
-----
  43
(1 row) 

select id, max((value->>'numberOfCarrot')::int)
from my_table,
jsonb_array_elements(jdata)
group by 1
order by 1;

 id | max 
----+-----
  1 |   2
  2 |   1
  3 |   1
  4 |   1
  5 |  43
  6 |   1
  7 |   1
  8 |   0
  9 |  23
 10 |  10
(10 rows)
如何在何处使用

select id, value->>'numberOfCarrot' as "numberOfCarrot"
from my_table,
jsonb_array_elements(jdata)
where (value->>'numberOfCarrot')::int > 20;

 id | numberOfCarrot 
----+----------------
  5 | 43
  9 | 23
(2 rows)

select id, array_agg(value->>'numberOfCarrot') as "numberOfCarrot"
from my_table,
jsonb_array_elements(jdata)
where (value->>'numberOfCarrot')::int > 2
group by 1
order by 1;

 id | numberOfCarrot 
----+----------------
  5 | {43}
  9 | {3,23}
 10 | {10}
(3 rows)