Sql 当第二个表中的布尔值为TRUE时,用第一个表覆盖第二个表的结果
我想要Sql 当第二个表中的布尔值为TRUE时,用第一个表覆盖第二个表的结果,sql,postgresql,Sql,Postgresql,我想要role\u cap表中所有user\u cap\u id的role\u id=8的存在的被的授予的具有id=21覆盖 结果应该是: role_cap enduser_cap _____________________________________ _____________ role_id user_cap_id is_present id
role\u cap
表中所有user\u cap\u id
的role\u id=8
的存在的被的授予的具有id=21
覆盖
结果应该是:
role_cap enduser_cap
_____________________________________ _____________
role_id user_cap_id is_present id user_cap_id is_granted
------------------------------------- -------------------------
8 29 false 17 50 true
7 32 true 21 30 true
8 30 false 66 20 false
8 13 true 21 29 true
8 11 false
我可以通过单独的查询获得它,如:
_________________________
user_cap_id
-------------------------
29
30
13
第二次查询的结果应合并到第一次查询中,以便:
“当第二个表中的布尔值为TRUE时,用第一个表覆盖第二个表的结果”
无论role_cap和enduser_cap表中的内容是什么,都应该成为结果的一部分。但是,如果角色\u cap有假条目,而终端用户\u cap有同一用户\u cap\u id的真条目,那么终端用户\u cap的真条目将被优先考虑。听起来像是在检查用户是否在id=21的终端用户\u cap表中为真或在角色\u cap本身中为真之间的或操作
select * from role_cap where role_id = 8
select * from enduser_cap where id = 21
如果要查看由最终用户\u cap记录(id=21)覆盖的来自角色\u cap(角色\u id=8)的记录,请使用:
select user_cap_id
from role_cap r
where role_id = 8
and (
exists (
select 1
from enduser_cap u
where u.user_cap_id = r.user_cap_id
and id = 21
and is_granted
)
or is_present
);
预期的输出对我来说没有意义。你能显示完整的表格吗?无论在role_cap和enduser_cap表格中是什么,都应该成为结果的一部分。但是,如果角色\u cap的输入为假,而终端用户\u cap的输入为真,则终端用户\u cap的输入为真将被视为优先级。@TimBiegeleisen上表示例已完成,但数据有所不同。看我上面的评论,很快。谢谢
SELECT r.user_cap_id
FROM role_cap r, enduser_cap e
WHERE r.role_id = 8 AND e.id=21 AND
r.user_cap_id=e.user_cap_id AND r.is_present <> e.is_granted
user_cap_id
------------
29
30