在我的PostgreSQL数据库中,我有以下架构:
CREATE TABLE survey_results (
id integer,
domain_name varchar,
scores jsonb DEFAULT '{}'::jsonb
);
INSERT INTO survey_results (id, domain_name, scores)
VALUES (1, 'food_insecurity','{"medica": { "categories": { "food_insecurity": "low", "housing": "high" } } }');
INSERT INTO survey_results (id, domain_name, scores)
VALUES (2, 'housing','{"medica": { "categories": { "food_insecurity": "low", "housing": "high" } } }');
现在我想要一个额外的列进行sql查询。此列应使用domain_name列值从scores#>>'{medica, categories}'
获得分数,因此结果应如下所示:
id | domain_score
----------------------------------
1 | low
2 | high
我如何在PostgreSQL中执行此操作?
这是db fiddle with my db schema:https://fullcalendar.io/docs/nextDayThreshold
答案 0 :(得分:2)
使用->
和->>
operators.您可以使用列名作为操作数,例如:
select *, scores->'medica'->'categories'->>domain_name as domain_scores
from survey_results;