You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PostgreSQL中根据Key从JSON字符串获取对应Value

问题描述

数据库表t1的data列存储着格式为[{"K":"V"}]的JSON字符串,尝试使用以下SQL语句根据Key获取对应Value时,出现语法错误:

select Value ->> 'Value'
from json_array_elements(select data from t1) Key
where Key->>'Key'='K';
可行解决方案
  1. 创建表:
CREATE TABLE t1 (
id int,
data  json
);
  1. 向t1表插入测试数据:
insert into t1 values
(1,'[{"K":"V"}]'),
(2,'[{"loadShortCut_AutoText":"both"}]'),
(3,'[{"P":"R"}]');
  1. 使用以下查询语句获取目标结果:
select row, row->>'loadShortCut_AutoText' as value
from t1, json_array_elements (t1.data) as row
WHERE t1.id=2;
  1. 查询结果:
{"loadShortCut_AutoText":"both"}    both

内容的提问来源于stack exchange,提问作者Shantilal Suthar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 10:07:08