Mysql 检索购买了13种以上不同产品但从未购买过相同产品的客户

Mysql 检索购买了13种以上不同产品但从未购买过相同产品的客户,mysql,sql-server,adventureworks,Mysql,Sql Server,Adventureworks,我试过这个。但我觉得这会让订购相同产品的人 SELECT DISTINCT Count(od.orderqty) OrderQty, c.customerid, od.productid FROM sales.customer c INNER JOIN sales.salesorderheader oh ON c.customerid = oh.customerid

我试过这个。但我觉得这会让订购相同产品的人

SELECT DISTINCT Count(od.orderqty) OrderQty, 
                c.customerid, 
                od.productid 
FROM   sales.customer c 
       INNER JOIN sales.salesorderheader oh 
               ON c.customerid = oh.customerid 
       INNER JOIN sales.salesorderdetail od 
               ON oh.salesorderid = od.salesorderid 
GROUP  BY od.productid, 
          c.customerid 
HAVING Count(od.productid) > 10 
ORDER  BY c.customerid 

不确定您使用的是哪种风格的SQL,但请尝试以下方法:

select  t.CustomerID
from    (
select  c.CustomerID
        , count(distinct od.ProductID) as DistinctCount
        , count(od.ProductID) as Count
from    Sales.Customer c
join    Sales.SalesOrderHeader oh
        on c.customerid = oh.customerid
join    Sales.SalesOrderDetail od
        on oh.SalesOrderID = od.SalesOrderID
group 
by      c.CustomerID 
) as t
where   t.DistinctCount = t.Count
and     t.DistinctCount > 13
order 
by      t.CustomerID

您应该添加
t.DistinctCount>13
,以筛选购买超过13种不同产品的客户products@AB_87很好看!谢谢