选择北风产品的区域信息

时间:2013-03-03 22:54:40

标签: sql northwind

我想在northwind数据库中执行以下操作:

显示客户在CA中放置的所有产品的产品名称 但我很困惑如何将产品客户联系起来。

4 个答案:

答案 0 :(得分:3)

根据我的回忆,您需要将products表加入ordersorderDetailscustomers表,以获取客户订购的产品列表CA

select distinct p.ProductName
from customers c
inner join orders o
  on c.customerId = o.customerId
inner join orderDetails od
  on o.orderId = od.orderid
inner join products p
  on od.productid = p.productid
where c.Region = 'CA'

或者您可以使用EXISTS

select p.ProductName
from products p
where exists (select od.productid
              from customers c
              inner join orders o
                on c.customerId = o.customerId
              inner join orderDetails od
                on o.orderId = od.orderid 
              where c.Region = 'CA'
                and p.productid = od.productid)

请参阅两个查询的SQL Fiddle with Demo of sample

答案 1 :(得分:0)

试试这个:

Select o.pdt_name From orders o 
Where o.cust_no in 
 (Select c.cust_no From customers c Where c.state = 'CA')
order by o.pdt_name

答案 2 :(得分:0)

以下为我完成了这份工作

Select distinct Products.ProductName From Products 
Where Products.ProductID in 
 (Select [Order Details].ProductID From [Order Details] Where [Order Details].OrderID in(select Orders.OrderID from Orders where Orders.CustomerID in(select Customers.CustomerID from Customers where Customers.Region='CA')))
 order by Products.ProductName;

答案 3 :(得分:0)

SELECT DISTINCT PRD.PRODUCTNAME 
FROM CUSTOMERS AS CUST
INNER JOIN ORDERS AS ORD
ON CUST.CUSTOMERID=ORD.CUSTOMERID
INNER JOIN [ORDER DETAILS] AS ORDDETAILS
ON ORD.ORDERID=ORDDETAILS.ORDERID
INNER JOIN PRODUCTS AS PRD
ON ORDDETAILS.PRODUCTID=PRD.PRODUCTID
WHERE CUST.REGION='CA'

按产品名称按升序列出结果,将其添加到结尾

ORDER BY PRD.PRODUCTNAME ASC