简体   繁体   English

如何过滤 Postgresql jsonb 像键内的查询键

[英]how to filter Postgresql jsonb like query key inside key

id        data
0a        {"name" : "x1", "source_extended_attributes": {"alexa_pageviews": 6000}}
7ee       {"name" : "x2", "source_extended_attributes": {"alexa_pageviews": 6000}}
8d        {"name" : "x3", "source_extended_attributes": {"alexa_pageviews": 6000}}

The data column is defined as jsonb. data列定义为 jsonb。

I need to use a select where alexa_pageviews but I can't.我需要使用 select 其中alexa_pageviews但我不能。

I can use name like where data->>'name' = 'x1'我可以使用name ,例如where data->>'name' = 'x1'

i need how access key inside key in PostgreSQL query我需要如何在 PostgreSQL 查询中访问密钥

Simple: data->'source_extended_attributes'->>'alexa_pageviews' If you want to compare the value with something, there are several ways.很简单: data->'source_extended_attributes'->>'alexa_pageviews'如果你想将值与某物进行比较,有几种方法。

As text (returns JSON value as text explicitly using ->> operator):作为文本(使用->>运算符显式返回 JSON 值作为文本):

data->'source_extended_attributes'->>'alexa_pageviews' like '6%'

As numeric (casts the TEXT representation to the NUMERIC value to the given type ( int )):作为数字(将 TEXT 表示形式转换为给定类型 ( int ) 的 NUMERIC 值):

(data->'source_extended_attributes'->>'alexa_pageviews')::int > 5000

As JSON numeric (implicitly casts righthand TEXT constant to JSON type):作为 JSON 数字(将右手文本常量隐式转换为 JSON 类型):

data->'source_extended_attributes'->'alexa_pageviews' > '999'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM