问题描述
我有一个如下所示的表格和两个用户 Soni
和 Garve
。我想根据用户的角色和使用 string_to_match
列中的值仅向用户显示少量订单。
用户 Soni
可以看到 order_id
如下所示,product_keys
的所有 order_id
如果 order_id
甚至有一个 string_to_match
=pending rows/订单。
类似的角色 Garve
可以看到 product_keys
的所有 order_id
,如果 order_id
有 string_to_match=waiting orders
。
在 Postgres 中可以吗?
解决方法
如果每个人都有一个角色 ID 并且你可以根据你的情况来决定,那么
-- roleid 1 = access to 'pending'
-- roleid 2 = access to 'waiting'
declare roleid integer;
select * from
yourordertable t1
where exists ( select 1 from yourordertable t2
where t2.order_id = t1.order_id
and case roleid
when 1 then t2.string_to_match = 'pending'
when 2 then t2.string_to_match = 'waiting'
)
and case roleid when 2 then t2.string_to_match <> 'pending'
不确定最后一个条件,但根据您最后的输出屏幕截图,它会过滤掉“等待”状态的“待处理”状态。如果不需要,您可以将其删除。