Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.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/4/json/13.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/1/visual-studio-2008/2.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 有没有办法使用matching子句在json postgres列中搜索?_Sql_Json_Postgresql - Fatal编程技术网

Sql 有没有办法使用matching子句在json postgres列中搜索?

Sql 有没有办法使用matching子句在json postgres列中搜索?,sql,json,postgresql,Sql,Json,Postgresql,我正在尝试从Postgres JSON列中搜索记录。存储的数据具有如下结构 { "contract_shipment_date": "2015-06-25T19:00:00.000Z", "contract_product_grid": [ { "product_name": "Axele", "quantity": 22.58 }, { "product_name": "Bell", "quantity": 52.

我正在尝试从Postgres JSON列中搜索记录。存储的数据具有如下结构

{
  "contract_shipment_date": "2015-06-25T19:00:00.000Z",
  "contract_product_grid": [
    {
      "product_name": "Axele",
      "quantity": 22.58
    },
    {
      "product_name": "Bell",
      "quantity": 52.58
    }
  ],
  "lc_status": "Awaited" 
}
我的表名是Heap,列名是contract\u product\u grid。此外,合同\产品\网格列可以包含多个产品记录。 我找到了此文档,但无法获得所需的输出

所需的情况是,我有一个过滤器,用户可以在其中选择产品名称,在使用matching子句输入名称的基础上,将获取记录并返回给用户。

假设您输入Axele作为产品名称输入,并希望返回数量键的匹配值

然后使用:

SELECT js2->'quantity' AS quantity
  FROM
  (
   SELECT JSON_ARRAY_ELEMENTS(value::json) AS js2
     FROM heap,
     JSON_EACH_TEXT(contract_product_grid) AS js
    WHERE key = 'contract_product_grid' 
  ) q
  WHERE js2->> 'product_name' = 'Axele' 
其中,通过JSON_EACH_TEXTjson将最外层的JSON扩展为键和值对,并通过JSON_array_ELEMENTSvalue::JSON函数使用新形成的数组拆分所有元素

然后,在主查询中按特定产品名称过滤掉


注意:不要忘记用大括号将JSON列的值括起来

您需要取消数组的测试,以便能够对每个值使用类似的条件:

select h.*
from heap h
where exists (select * 
              from jsonb_array_elements(h.contract_product_grid -> 'contract_product_grid') as p(prod)
              where p.prod ->> 'product_name' like 'Axe%')
如果您真的不需要通配符搜索so=而不是LIKE,您可以使用contains操作符@>,这将更加高效:

select h.*
from heap h
where h.contract_product_grid -> 'contract_product_grid' @> '[{"product_name": "Axele"}]';
这也可用于搜索多个产品:

select h.*
from heap h
where h.contract_product_grid -> 'contract_product_grid' @> '[{"product_name": "Axele"}, {"product_name": "Bell"}]';
如果您使用的是Postgres 12,您可以使用表达式将其简化:

select *
from heap
where jsonb_path_exists(contract_product_grid, '$.contract_product_grid[*].product_name ? (@ starts with "Axe")')
select *
from heap
where jsonb_path_exists(contract_product_grid, '$.contract_product_grid[*].product_name ? (@ like_regex "axe.*" flag "i")')
或使用正则表达式:

select *
from heap
where jsonb_path_exists(contract_product_grid, '$.contract_product_grid[*].product_name ? (@ starts with "Axe")')
select *
from heap
where jsonb_path_exists(contract_product_grid, '$.contract_product_grid[*].product_name ? (@ like_regex "axe.*" flag "i")')
挑选* 从…起 选择JSON\数组\元素合同\产品\网格::JSON作为js2 成堆 其中“合同产品网格”=“合同产品网格” Q 其中js2->>“产品名称”位于“Axele”、“Bell” 正如我在问题中提到的,我的列名是“contract\u product\u grid”,我只需要从中搜索。
使用此查询,我可以使用IN子句和输入的产品名称获取合同产品网格信息。

查看文档,非常清楚。再看一看这个例子,它与您的问题非常相似:您希望在etcThanks中使用=进行精确匹配,还是使用类似的通配符进行通配符匹配,但它给出了以下错误:错误:无法将数组解构为对象您的DBMS版本是什么@muhammadisrashahid?你看过演示了吗?但它没有使用通配符。如果不需要通配符搜索,那么使用@>运算符将更加有效。