我有一个名为Orders的表,它包含CUSTNUM,PRODNUM,DATE和QTY列。
我正在尝试检索订购产品3737和产品9193(特定值)的客户
所以我尝试了以下代码但没有成功:
SELECT CUSTNUM
FROM ORDERS
WHERE PRODNUM = 9193
INTERSECT
(SELECT CUSTNUM
FROM ORDERS
WHERE PRODNUM = 3737);
在此论坛上进一步阅读之后,我也尝试了这个:
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
);
尽管INTERSECT运算符包含在我们的教科书中,但似乎无法正常工作。
我也咨询了INTERSECT in MySQL
答案 0 :(得分:1)
MySQL支持UNION [ALL],但不支持MINUS / EXCEPT和INTERSECT。
select custnum
from orders
where prodnum in (3737,9193)
group by custnum
having count(distinct prodnum) = 2
或
select custnum
from orders
where prodnum in (3737,9193)
group by custnum
having min(prodnum) = 3737
and max(prodnum) = 9193