我在这个家庭作业问题上遇到了一些问题:
我已经能够成功完成几乎所有的查询,除了“仅包含至少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
答案 0 :(得分:1)
如果您的尝试工作正常,则查询后面提到的条件应该有效:
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;
它会算不上。每个州的商品,因为你已经拥有国家分组。
希望它有所帮助!
答案 1 :(得分:0)
使用HAVING
子句来计算和断言钻石矿的数量:
SELECT
I.state,
SUM(P.capacity)
FROM Infrastructure I
NATURAL JOIN Mine M
INNER JOIN produces P
ON P.mine = M.entryno
INNER JOIN Commodity C
ON C.comID = P.commodity
WHERE
C.name LIKE '%Diamond%'
GROUP BY
I.state
HAVING
COUNT(*) >= 3;
注意:自然连接似乎容易出错,并且可能会在某些时候中断。最好的用法是显式连接。