简体   繁体   English

Postgres查询以返回其中json列中json包含多个数组元素的行

[英]Postgres query to return rows where json in the json column contains array elements more than one

We are using Postgres DB , in that we have one table contains column of type JSON , format is like below 我们正在使用Postgres DB,因为我们有一个表包含JSON类型的列,格式如下

{
  "name" : "XXX",
  "id"  : "123",
  "course" :[
     {
       "name" : "java",
       "tutor":"YYYY"
     },
     {
       "name" : "python",
       "tutor":"ZZZZ"
     }

   ]  

}


{
  "name" : "XXX",
  "id"  : "123",
  "course" :[
     {
       "name" : "java",
       "tutor":"YYYY"
     },
     {
       "name" : "python",
       "tutor":"ZZZZ"
     }

   ]  

}

like this for example we have two rows , and in the json column we have each like above 例如,我们有两行,在json列中,每行都像上面一样

i want to Postgre query , which will check the number of elements in the course array and if it is more than one , then only return that row 我想Postgre查询,它将检查课程数组中的元素数,如果大于一个,则仅返回该行

am not getting how to count the array elements from inside the json key 没有得到如何从json键内部计算数组元素

can any please suggest 可以请建议吗

why not just json_array_length ?.. eg: 为什么不只是 json_array_length ?..例如:

f=# with c(j) as (values('{
  "name" : "XXX",
  "id"  : "123",
  "course" :[
     {
       "name" : "java",
       "tutor":"YYYY"
     },
     {
       "name" : "python",
       "tutor":"ZZZZ"
     }

   ]

}'::json))
select json_array_length(j->'course') from c;
 json_array_length
-------------------
                 2
(1 row)

so smth like

select * from table_name where json_array_length(j->'course') > 1

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

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