在postgres中的json列中通过多个键和值查找行

时间:2019-06-27 01:10:13

标签: json postgresql select

我有一个包含json列的表,我只想像这样向json中获取具有特定键和值的行:

这是当我执行简单的选择查询时来自该列的值的示例:

select my_atribute from displays;

结果:

   {"key": "unit_type", "value": "Tablet", "display_name": "Unit Type"}
   {"key": "pack_type", "value": "Packet", "display_name": "Pack Type"}
   {"key": "units_in_pack", "value": "60", "display_name": "Units in Pack"}
   {"key": "item_unit", "value": "", "display_name": "Item unit"}
   {"key": "item_size", "value": "1", "display_name": "Item Size"}
   {"key": "details", "value": "", "display_name": "Details"}
   {"key": "slug", "value": "otc7087", "display_name": "Slug"}

我只希望具有“键”(例如“子弹”或“详细信息”)和“显示名称”(例如“项目单位”或“详细信息”)的行

我已经尝试过以下代码:

WITH my_table(jsonblob) AS (VALUES ((select my_atribute from displays) :: jsonb))
SELECT elem ->> 'value'
FROM my_table
       CROSS JOIN LATERAL jsonb_array_elements(jsonblob) elem
WHERE ((elem ->> 'key') in ('slug', 'details'))
  and ((elem ->> 'display_name') in ('Item unit', 'Details'));

这是我收到的消息: 错误:由子查询返回的多于一行用作表达式

0 个答案:

没有答案