oracle sql developer查询3个表

时间:2014-02-05 17:44:09

标签: sql oracle

在oracle sql中我有这3个包含名称和列的表: 1.orders:
顾客ID orderid(主键)

2.orderlines: orderlineid(主键) 订单ID 的productid

3.产品: prodid(主键) 类

我希望找到至少包含1个订单的客户(customerid),其中包含来自至少3个不同类别的产品。如果有人可以帮我解决这个问题。非常感谢!

尝试了很多东西,但没有任何效果,因为我是SQL的新手,这里有一个例子,因为我不知道我在做什么,所以不要太注意了。再次感谢。

select customerid,count(orderid) as total
from (select customerid,orderid,prodid,distinct category
from orders o,orderlines n,products p
where o.orderid=n.orderid and n.prodid=p.prodid )
group by (customerid);

1 个答案:

答案 0 :(得分:0)

SELECT DISTINCT customerid 
FROM ORDERS O
INNER JOIN PRODUCTS P
ON (P.prodid = O.prodid)
INNER JOIN ORDERLINES OL
ON (O.prodid = OL.prodid AND O.orderid = P.orderid)
GROUP BY O.customerid,O.orderid
HAVING
COUNT(DISTINCT p.category) >= 3

以你的JOINS风格(我最喜欢的)

SELECT DISTINCT customerid 
FROM ORDERS O,PRODUCTS P,ORDERLINES OL
WHERE (O.prodid = P.prodid)
AND (O.prodid = OL.prodid AND O.orderid = P.orderid)
GROUP BY O.customerid,O.orderid
HAVING COUNT(DISTINCT p.category) >= 3