我有一个名为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

最佳答案

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

关于mysql - INTERSECT在MYSQL编码中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41342911/

10-10 03:35