繁体   English   中英

Postgres:jsonb 对象上的多个条件

[英]Postgres: Multiple conditions on jsonb object

这是大约有 200,000 行的数据集

"test"  "test"  "[{""field"": ""devops"", ""years"": 8}, {""field"": ""backend dev"", ""years"": 3}]"
"test"  "test"  "[{""field"": ""devops"", ""years"": 9}, {""field"": ""backend dev"", ""years"": 4}]"
"test"  "test"  "[{""field"": ""devops"", ""years"": 9}, {""field"": ""backend dev"", ""years"": 9}]"

我想找到所有具有以下经验的用户:

  • 开发运营 = +5 年
  • 后端开发 = +5 年

一个条件在我的查询中起作用

with parsed_exp as ( 
 select 
 "firstName", 
 "lastName", 
 experience, 
 jsonb_array_elements(experience) as e from users )
select * from parsed_exp 
where (e->>'field') = 'backend dev' and (e->>'years')::integer > 5 

但是两个条件一起不起作用:

with parsed_exp as ( 
 select 
 "firstName", 
 "lastName", 
 experience, 
 jsonb_array_elements(experience) as e from users )
select * from parsed_exp 
where 
(e->>'field') = 'backend dev' and (e->>'years')::integer > 5 and 
(e->>'field') = 'devops' and (e->>'years')::integer > 5

^^它返回 0 行

如果您使用的是 Postgres 12 或更高版本,则可以使用 JSON 路径表达式:

select *
from users 
where jsonb_path_exists(experience, '$[*] ? (@.field == "devops" && @.years > 5)')
  and jsonb_path_exists(experience, '$[*] ? (@.field == "backend dev" && @.years > 5)')

暂无
暂无

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

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