简体   繁体   English

oracle sql developer查询3个表

[英]oracle sql developer query for 3 tables

in oracle sql i have this 3 tables with names and columns: 1.orders: 在Oracle SQL中,我有这3个表的名称和列:1.orders:
customerid orderid (primary key) customerid orderid(主键)

2.orderlines: orderlineid (primary key) orderid productid 2.orderlines:orderlineid(主键)orderid productid

3.products: prodid (primary key) category 3.产品:产品(主键)类别

and i want to find the customers(customerid) that have make at least 1 order which contains products from at least 3 different categories.if someone can help me with this query.thanks a lot! 并且我想找到至少已下达1个订单的客户(customerid),其中包含至少3个不同类别的产品。如果有人可以帮助我进行此查询。非常感谢!

tried a lot of things but nothing worked since i am a newbie in SQL here is an example,since i dont know what i am really doing dont give it to much attention.thank again. 尝试了很多事情,但是没有任何效果,因为我是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);
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

In Your Style of JOINS (My Favourite too) 以您的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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM