我试着在postgres下面查询:
select * from user u join
user_func uf on u.pk = uf.user_pk
where uf.functions in ('UPDATE_MODEL', 'UPDATE_USER')
and u.perfil in ('ADMIN', 'MANAGER');
但是这会返回UPDATE_MODEL和UPDATE_USER中的任何匹配,当用户在user_func中有两个函数时,我只想要所有匹配。你帮我吗?感谢。
例如。用户ID = 1在user_func中只有UPDATE_MODEL,它出现在结果中,对我来说是错误的。 此外,用户ID = 2会显示在结果中,但是正确,因为它具有所有功能(' UPDATE_MODEL',' UPDATE_USER')。
答案 0 :(得分:1)
select * from user u
join user_func uf on u.pk = uf.user_pk
join user u2 on u.id = u2.id
join user_func uf2 on u2.pk = uf2.user_pk
where uf.functions = 'UPDATE_MODEL'
AND uf2.functions = 'UPDATE_USER'
AND u.perfil in ('ADMIN', 'MANAGER');
答案 1 :(得分:1)
SELECT * FROM users
WHERE u IN (SELECT u from user_func
WHERE functions IN ('UPDATE_MODEL', 'UPDATE_USER')
group by u
having (count(*) > 1) )
AND perfil in ('ADMIN', 'MANAGER');
看看这是否有效。
答案 2 :(得分:1)
select * from user u join
(
select uf1.* from user_func uf1
join user_func uf2 on uf1.user_pk=uf2.user_pk and uf1.functions <> uf2.functions
where uf1.functions in('UPDATE_MODEL', 'UPDATE_USER')
) as T
on u.pk = T.user_pk
where and u.perfil in ('ADMIN', 'MANAGER');