MYSQL 查询 - 组合
MYSQL Queries - combination
我需要找到购买了最贵产品的客户的名字和姓氏。
架构
订单
- 订单号
- 订单类型ID
- 客户ID
- 数量
- 购买日期
客户
- 客户ID
- 状态
- 邮政编码
- 街道编号
- 街道地址
- 客户Ln
- 客户Fn
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))
我需要找到购买了最贵产品的客户的名字和姓氏。
架构
订单
- 订单号
- 订单类型ID
- 客户ID
- 数量
- 购买日期
客户
- 客户ID
- 状态
- 邮政编码
- 街道编号
- 街道地址
- 客户Ln
- 客户Fn
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))