[英]Query Json field on postgresql
我需要使用 localId 搜索键,我尝试了这个查询:
SELECT *
FROM public.translations
where datas->>'localId' = 7;
SELECT *
FROM public.translations
where datas->>'localId'::text = '7';
没有结果。
请问我该怎么做?
当我进行此查询时,我没有任何值
SELECT datas->>'localId' as local
FROM public.translations
SELECT datas::json->>'localId' as local
FROM public.translations
您的屏幕截图有点难以阅读,但您的 JSON 实际上是 JSON数组,因此您需要从那里选择第一个元素:
where (datas -> 0 ->> 'localId')::int = 7
或者更短一点:
where (datas #>> '{0,localId}')::int = 7
或者,您可以使用 contains 运算符@>
检查是否至少有一个元素具有localId = 7
。 但是@>
运算符需要jsonb
,而不是json
,因此您需要转换列
where datas::jsonb @> '[{"localId": 7}]'
您需要的是::json
datas
之后:
SELECT *
FROM public.translations
where datas::json->>'localId' = '7';
如果您需要更多信息,请参阅最新的 Postgresql 文档
您需要将两个值都转换为相同的类型
where datas->>'localId' = '7'::text;
或者
where (datas->>'localId')::integer = 7;
或者你应该为你的例子添加括号
where (datas->>'localId')::text = '7';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.