If I have a table1 with columns table1.user and table1.site, how can I return a list of distinct users that have access to ALL of a list of sites?
Let me clarify. I could start with the following code:
SELECT DISTINCT user
FROM table1
WHERE (site IN ('site1','site2','site3'))
Of course, this will display all distinct users that have entries for ANY of the three listed sites. I only want the users who have entries for ALL of the three listed sites.
I feel like there’s probably an obvious way to do this, and I’m probably going to feel quite stupid once someone points it out. Still, I’m drawing a blank.
This assumes that the user cannot be assigned to the same site more than once. If that is not the case, than Gordon’s comment holds true, and the having expression must be replaced with
HAVING count(distinct site) = 3A more dynamic approach where the sites are only specified once and the count() adjusts automatically could be this:
(This is ANSI SQL, as no DBMS has been specified)