微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用同一个表中的值在表上应用 RLS

如何解决如何使用同一个表中的值在表上应用 RLS

我有一个如下所示的表格和两个用户 SoniGarve。我想根据用户的角色和使用 string_to_match 列中的值仅向用户显示少量订单。

enter image description here

用户 Soni 可以看到 order_id 如下所示,product_keys 的所有 order_id 如果 order_id 甚至有一个 string_to_match=pending rows/订单。

enter image description here

类似的角色 Garve 可以看到 product_keys 的所有 order_id,如果 order_idstring_to_match=waiting orders

enter image description here

在 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' 

不确定最后一个条件,但根据您最后的输出屏幕截图,它会过滤掉“等待”状态的“待处理”状态。如果不需要,您可以将其删除。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。