我有两个表'user_rights'作为父表,'assigned_rights'作为子表。我需要在表'assigned_rights'中显示没有子项的所有权限,如果有孩子,我必须检查其中一个孩子是否不等于user_id。
SELECT ur.right_id,
ur.right_name,
ur.right_description,
ur.right_level,
ura.user_id
from user_rights as ur
left join assigned_rights as ura
on ur.right_id = ura.right_id
where ura.user_id is null or ura.user_id <> 'abc'
如果没有孩子,并且子节点和用户ID匹配字段'user_id',我的选择工作正常。问题是,如果有不同用户ID的孩子。
答案 0 :(得分:0)
如果您希望与孩子相关,请使用内部联接
SELECT ur.right_id,
ur.right_name,
ur.right_description,
ur.right_level,
ura.user_id
from user_rights as ur
inner join join assigned_rights as ura
on ur.right_id = ura.right_id
where ura.user_id <> 'abc'
答案 1 :(得分:0)
SELECT ur.right_id,
ur.right_name,
ur.right_description,
ur.right_level,
ura.user_id
from user_rights as ur
left join assigned_rights as ura
on ur.right_id = ura.right_id and ura.user_id = 'abc'