MYSQL 查询 - 组合

MYSQL Queries - combination

我需要找到购买了最贵产品的客户的名字和姓氏。

架构


订单

客户

Order_Contents

产品

我该如何编写这个查询?

像这样的事情似乎应该作为一个起点(这里完全是天真的方法)

SELECT CustomerFn, CustomerLn FROM Customers c
  LEFT JOIN Orders o on o.customerID = c.customerID
  LEFT JOIN Order_Contents oc on oc.orderID = o.orderID
  LEFT JOIN Products p on p.productID = oc.productID
  WHERE oc.productID IN (
    SELECT productID FROM Products WHERE unitPrice = MAX(unitPrice)
  );
   select c.customerFn, c.customerLn
   from customers c join orders o where o.customerID = c.customerID
   join order_contents l where l.orderId = o.orderId
   join products p where p.productId = l.productId
   order by p.unitPrice desc
   limit 1

您可以使用如下子查询。

select 
   Customers.customerFN,
   Customers.customerLN
from
    Customers
    inner join Orders  on Orders.customerID = Customers.customerID
   inner join Order_Contents on Order_Contents.orderID = Orders.orderID 
where
    Order_Contents.productID in
    (select Products.productID from Products where Products.unitPrice = (select max(Products.unitPrice) from Products))