gpt4 book ai didi

mysql - 如何通过多个关系过滤 SQL 查询?

转载 作者:行者123 更新时间:2023-11-29 13:17:29 25 4
gpt4 key购买 nike

我在做这个家庭作业时遇到了一些问题:

enter image description here

除了“仅包括至少拥有 3 个钻石矿的州”之外,我已经能够成功完成几乎所有查询。我想问一下如何将这部分添加到查询中。

select I.state, sum(P.capacity)
from Infrastructure I
natural join Mine M
join produces P
on P.mine = M.entryno
join Commodity C
on C.comID = P.commodity
where C.name like '%Diamond%'
group by I.state

最佳答案

如果您的尝试工作正常,除了问题中提到的条件之外,以下查询应该工作:

select I.state, sum(P.capacity)
from Infrastructure I
natural join Mine M
join produces P
on P.mine = M.entryno
join Commodity C
on C.comID = P.commodity
where C.name like '%Diamond%'
group by I.state
having count(P.mine) >=3;

它会计算编号。每个州的商品数量,因为您已经对州进行了分组。

希望对您有所帮助!

关于mysql - 如何通过多个关系过滤 SQL 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46908031/

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