gpt4 book ai didi

mysql - 查询查找两个商品之间区域的最低总价

转载 作者:行者123 更新时间:2023-11-29 07:48:04 24 4
gpt4 key购买 nike

这是一个包含示例元素的(简化的)表格:

Item      Area      Price
Gas Miami 6.00
Gas LA 8.00
Gas Dallas 2.00
Corn Miami 2.00
Corn LA 2.00
Corn Dallas 5.00
Oil Miami 8.00
Oil LA 9.00
Oil Dallas 3.00

我想要 Jade 米和天然气成本最低的地区。因此,在此表中,答案将是达拉斯,因为 Jade 米和天然气的价格总和为 7.00(迈阿密为 8.00,洛杉矶为 10.00)。如果有任何关于如何完成这项工作的想法,我将不胜感激。

(注意:我只给出石油价格,因此您知道列表中还有无数的项目,因为每个项目都有更多的区域。)

最佳答案

这是一个带有order by的聚合查询:

select area, sum(price) as sumprice
from simplifiedtable t
where item in ('gas', 'corn')
group by area
having sum(item = 'gas') = 1 and sum(item = 'corn') = 1
order by sum(price) asc
limit 1;

请注意,having 子句确保这些区域具有这两种产品。

关于mysql - 查询查找两个商品之间区域的最低总价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27193865/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com