mysql - 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 粉丝,我被卡住了!
解决方案
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
推荐阅读
- windows - 无论如何要调试带有安全启动的Windows内核吗?
- javascript - AG-Grid 拖放单元格
- docker - 使用 CircleCi 自动化 AWS EC2 中的 docker 容器
- vue.js - 如何设置离子输入的值?
- python-3.x - 通过有状态对象获取渐变
- kotlin - 在基类初始化(直接或间接)中使用覆盖属性的示例是什么?
- c - 手动获取指针的值 | 错误:应忽略的无效值
- c# - 在我们中间构建 C# 时生成引用错误 mod
- reactjs - 将材质ui功能组件迷你变体抽屉转换为类组件Reactjs
- python - 如何以编程方式告知 AWS Step Function 执行已完成?