我有一个jsonb
列,其中包含有关促销促销的详细信息。
我想将键筛选为预定义列表中的键。
我应用了对我来说很直观的where code in ('foo', 'bar')
,但出现了一个错误:ANY/ALL (array) does not support set arguments
有什么想法吗?
查询:
select
upper(jsonb_object_keys(orders.extra_details -> 'promotion_codes')) as promo_code,
count(tickets.id)
from
tickets
inner join order_items on order_items.orderable_id = tickets.id
inner join orders on orders.id = order_items.order_id
where
order_items.type = 'TicketOrderItem'
and orders.extra_details -> 'promotion_codes' is not null
and tickets.event_id = 4062
and tickets.status = 2
and upper(jsonb_object_keys(orders.extra_details -> 'promotion_codes')) in ('FOO', 'BAR', 'BAZ')
group by
1
order by
1
答案 0 :(得分:1)
jsonb_object_keys
返回值的集。您不应该在select
列表中使用它,也不能在where子句中真正使用它。
就像使用这样的表达式:where (select some_column from some_table) in ('a','b')
正确的使用方式是针对jsonb_object_keys
select upper(ed.promo_code) as promo_code,
count(tickets.id)
from tickets
join order_items on order_items.orderable_id = tickets.id
join orders on orders.id = order_items.order_id
join lateral jsonb_object_keys(orders.extra_details -> 'promotion_codes') as ed(promo_code) on true
where
order_items.type = 'TicketOrderItem'
and orders.extra_details -> 'promotion_codes' is not null
and tickets.event_id = 4062
and tickets.status = 2
and upper(ed.promo_code) in ('FOO', 'BAR', 'BAZ')
group by
1
order by
1