SQL子查询 - 显示所有运送产品的国家/地区

问题描述 投票:-1回答:3

写一个查询,以显示所有运送产品的国家:Sir Rodney's Scones。

产品名称'Sir Rodney's Scones'和各国都在不同的表格中。这是我到目前为止所尝试的:

select country
from customers
where country =any (select productname
            from products
            where productname = 'Sir Rodney''s Scones'
             )

我知道multirow运算符是错误的,但我不知道用什么来代替它。它也必须是一个子查询。

sql oracle subquery
3个回答
1
投票

尝试

select country
from customers
where country in (select country
            from products
            where productname  in ('xyz','pqr')
             )

注意:您可以将'xyz'或'pqr'替换为您的产品名称


0
投票

您可以检查两种产品的产品中是否存在国家/地区。

SELECT DISTINCT country
FROM customers A
WHERE EXISTS (SELECT 1 FROM products B WHERE B.productName='Sir Rodney''s Scones'
              AND B.country=A.country);

现在更正您的查询:您的子查询应该返回一个国家/地区。

select country
from customers
where country in (select country
            from products
            where productname='Sir Rodney''s Scones'
             );

0
投票

您应该加入客户订单详细信息,以了解客户购买该产品的信息。并使用distinct来获得独特的国家

  select    distinct Country
  from      Customers c
  join      Orders o on c.CustomerID = o.CustomerID
  join      OrderDetails od on o.OrderID = od.OrderID
  join      Products p on od.ProductID = p.ProductID
  where     p.ProductName = 'Sir Rodney''s Scones'

或者您可以使用订单表中的ShipCountry。

  select    distinct ShipCountry
  from      Orders o
  join      OrderDetails od on o.OrderID = od.OrderID
  join      Products p on od.ProductID = p.ProductID
  where     p.ProductName = 'Sir Rodney''s Scones'

如果你需要子查询

  select    distinct ShipCountry
  from      Orders o
  where     o.OrderID in (
                select      OrderID 
                from        OrderDetails od
                where       od.ProductID in (
                                select      ProductID
                                from        Products p
                                where       p.ProductName = 'Sir Rodney''s Scones'
                            )
            )
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.