首页 > 解决方案 > MySQL ,哪个卖家在给定产品列表中的销售额最高

问题描述

所以,我有 2 张桌子

卖家表

id | seller_name

售出的产品

(请注意在 sold_products 中可以有多个相同product_name 、相同seller_id但不同的条目quantity_sold):

id | seller_id | product_name | quantity_sold
1  | 1         | cabbage      | 4
1  | 1         | cabbage      | 6
1  | 3         | cabbage      | 1
1  | 3         | cabbage      | 3
1  | 1         | tomatoes     | 4
1  | 1         | tomatoes     | 2
1  | 1         | potatoes     | 4
1  | 3         | tomatoes     | 1
1  | 3         | tomatoes     | 1
1  | 3         | potatoes     | 2

我想得到出售这些给定项目名称中最多的卖家: potatoes, tomatoes, cabbage,所以这些必须包含在 WHERE 子句或其他方式中......

你们将如何编写查询,这里不是 SQL 粉丝,我被卡住了!

标签: mysqlsql

解决方案


select seller_id
from sold_products
where product_name in ('potatoes', 'tomatoes', 'cabbage')
group by seller_id
order by sum(quantity_sold) desc
limit 1

推荐阅读