Alex Cutajar
Alex Cutajar

Reputation: 29

INTERSECT in MYSQL coding not working

I have a table called Orders which consists of columns CUSTNUM, PRODNUM, DATE and QTY.

I am trying to retrieve the customers who ordered both products 3737 and products 9193 (particular values)

So I tried the following code with no success:

SELECT CUSTNUM
FROM ORDERS
WHERE PRODNUM = 9193

INTERSECT

(SELECT CUSTNUM
FROM ORDERS
WHERE PRODNUM = 3737);

After further reading also on this forum, I also tried this:

SELECT DISTINCT CUSTNUM
FROM ORDERS
WHERE PRODNUM IN (
select PRODNUM FROM ORDERS WHERE PRODNUM = 3737
) AND PRODNUM IN (
Select PRODNUM FROM ORDERS WHERE PRODNUM = 2357
);

The INTERSECT operator seems not to work although it is included in our textbooks.

I've also consulted INTERSECT in MySQL

Upvotes: 2

Views: 1663

Answers (1)

David דודו Markovitz
David דודו Markovitz

Reputation: 44921

MySQL supports UNION [ALL] but not MINUS/EXCEPT and INTERSECT.

select    custnum            
from      orders    
where     prodnum in (3737,9193)    
group by  custnum       
having    count(distinct prodnum) = 2

or

select    custnum            
from      orders    
where     prodnum in (3737,9193)    
group by  custnum       
having    min(prodnum) = 3737
      and max(prodnum) = 9193

Upvotes: 2

Related Questions