Mysql
 sql >> Database >  >> RDS >> Mysql

Query per utenti non in un gruppo specifico? (Volevo usare EXCEPT ma sembra che MySQL non lo supporti)

Puoi utilizzare un NOT IN() o un <> ANY()

SELECT CaseId
FROM CaseIdUsers
WHERE CaseID NOT IN (
  SELECT CaseId 
  FROM CaseIdUsers
    JOIN GroupMembers ON GroupMembers.User = CaseIdUsers.Sid
    JOIN Groups ON GroupMembers.Group = Groups.Sid
  WHERE Groups.Name = 'MyGroupName'
)

In alternativa, puoi utilizzare un LEFT JOIN con un GROUP BY

SELECT CaseId 
FROM CaseIdUsers
  LEFT JOIN GroupMembers ON GroupMembers.User = CaseIdUsers.Sid
  LEFT JOIN Groups ON GroupMembers.Group = Groups.Sid
    AND Groups.Name = 'MyGroupName'
HAVING COUNT(Groups.Sid) = 0