[英]Who bought the biggest amount of a product in one order
I have 2 tables我有 2 张桌子
CUSTOMERS
(ID, FIRSTNAME, LASTNAME, ADDRESS); CUSTOMERS
(ID、名字、姓氏、地址);ORDERS
(ID, PRODUCT_NAME, PRODUCT_PRICE, DATE_ORDER DATE, ID_CUSTOMER, AMOUNT); ORDERS
(ID、PRODUCT_NAME、PRODUCT_PRICE、DATE_ORDER DATE、ID_CUSTOMER、AMOUNT); Get the first and last names of the customers who bought the biggest amount of a product in one order.获取在一个订单中购买最多产品的客户的名字和姓氏。
The orders without customer should not be considered.不考虑没有客户的订单。 Please sort by FIRSTNAME and LASTNAME
请按 FIRSTNAME 和 LASTNAME 排序
Select firstname, lastname, amount
from customers
left Join orders on customers.id = orders.id_customer
group by firstname,lastname
having max(amount);
What am I missing?我错过了什么? Thanks
谢谢
There are many ways to skin this cat.有很多方法可以给这只猫剥皮。 This is one possibility:
这是一种可能性:
select distinct c.firstname,c.lastname,o.amount
from customers c
left join orders o on o.customer_id = c.id
where o.amount = (select max(amount)
from orders
)
There may be more than one row returned if multiple customers share the maximum order amount如果多个客户共享最大订单金额,则可能会返回多行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.