简体   繁体   English

谁在一个订单中购买了最大数量的产品

[英]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.

相关问题 哪些客户在一个订单中购买了最多的产品? - What customers who bought the biggest amount of a product in one order? 如何找到购买一种产品而不是另一种产品的人? - How to find people who bought one product but NOT another? MySQL:获取购买产品 a 后购买产品 b 的用户数量 - MySQL: Get number of users who bought product b AFTER they bought product a 如何让购买了 x 产品的客户也购买了 y 产品 - How to get the customers who bought x product also bought y product 如何找到购买产品A和D> 6个月的客户? - How find Customers who Bought Product A and D > 6 months apart? SQL查询以查找购买了他之前从未购买过的新产品的零售商 - Sql query to find the retailers who bought new product which he had not bought before MYSQL:如何 Select 购买了一种产品但未购买另一种产品的客户 - MYSQL: How to Select Customers That Bought One Product But Not Another SQL Server如何查找从EACH商店购买产品的客户? - SQL Server How to Find the customers who have bought a product from EACH store? SQL查询统计在一天中购买了3种以上产品的人一天购买了多少种产品 - SQL Query count how many products bought on one day by those who bought more than 3 products on a different day 购买了 2 件商品的顾客 - Customers who bought the exact 2 items
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM