Given the following sample table schema
Customer Table
CustID
1
2
3
Invoice Table
CustID InvoiceID
1 10
1
Use:
SELECT c.custid
FROM CUSTOMER c
JOIN INVOICE i ON i.custid = c.custid
WHERE i.invoiceid IN (10, 20)
GROUP BY c.custid
HAVING COUNT(DISTINCT i.invoiceid) = 2
The key thing is that the counting of i.invoiceid
needs to equal the number of arguments in the IN
clause.
The use of COUNT(DISTINCT i.invoiceid)
is in case there isn't a unique constraint on the combination of custid and invoiceid -- if there's no chance of duplicates you can omit the DISTINCT from the query:
SELECT c.custid
FROM CUSTOMER c
JOIN INVOICE i ON i.custid = c.custid
WHERE i.invoiceid IN (10, 20)
GROUP BY c.custid
HAVING COUNT(i.invoiceid) = 2