简体   繁体   English

如何在具有一列作为 json 值的 postgres 数据库中进行查询

[英]How to query inside a postgres db having one column as json value

I have a table named Test and in that one column (Subject) contains JSON values.我有一个名为 Test 的表,其中一列(主题)包含 JSON 值。

This is the query which i am using这是我正在使用的查询

select Name,Subject 
from Test 
where id =1;

And the following are the JSON values present inside table.以下是表中存在的 JSON 值。

{
   "subject":{
      "Maths":"20",
      "Physics":"21",
      "English":"22"
   },
   "Staff":{
      "English":"Anna",
      "maths":"Rahul",
      "Physics":"John"
   }
}

Now my question is how to write a query to get English mark from JSON value.现在我的问题是如何编写查询以从 JSON 值中获取英文标记。 Expected o/p is 22. I am new to postgres, can any one help me in this thanks in advance预期的 o/p 是 22。我是 postgres 的新手,任何人都可以帮助我提前谢谢

You can combine the -> and ->> operators您可以组合->->> 运算符

select Name,Subject, subject -> 'subject' ->> 'English' as english_mark
from Test 
where id =1;

Alternatively use the #>> operator where you provide the path to the element you want as an array of keys:或者使用#>>运算符,您可以在其中提供所需元素的路径作为键数组:

select Name,Subject, subject #>> '{subject, English}' as english_mark
from Test 
where id =1;

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

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