简体   繁体   中英

SQL query all items match

I tried query bellow at 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');

But this return any match in UPDATE_MODEL and UPDATE_USER, I want only all match, when user has two functions in user_func. Do you help me? thanks.

For example. User id = 1 have only UPDATE_MODEL in user_func, and it appears in the result, it's wrong for me. Also the user id = 2 appears in the result, but is correct because it have all functions ('UPDATE_MODEL', 'UPDATE_USER').

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');
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');

see if this works.

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM