Microsoft Sql Server 2017,数据库罗斯文。
大家好,我仍在尝试在sql server中做这些问题,但是我叠了一下,不知道该怎么做。请给我一些提示,如何咬它:P
select *
from
(select companyname, products.productid, productname,[Order Details].OrderID
from [order details], orders, customers, products
where [order details].orderid=orders.orderid AND
orders.customerid=customers.customerid AND
[order details].productid=products.productid) c1
inner join
(select companyname, products.productid, productname, [Order Details].OrderID
from [order details], orders, customers, products
where [order details].orderid=orders.orderid AND
orders.customerid=customers.customerid AND
[order details].productid=products.productid AND
Customers.Country='Brazil') c2
on c1.productId=c2.productId and c1.productname=c2.productname
答案 0 :(得分:0)
我不确定是否应该从结果数据中排除巴西的客户,但是为了指出正确的方向,请考虑以下查询:
select c.*
from
(
select distinct o.productid
from [order details] o inner join customers c
on o.customerid = c.customerid
where c.country = 'Brazil'
) p
inner join [order details] o on p.productid = o.productid
inner join customers c on o.customerid = c.customerid
在这里,嵌套选择获得了巴西客户购买的一组独特的产品。然后,选择结果将添加到您的order details
表中,以获取购买了此类产品的所有客户(其中包括巴西人)。