在我的数据库项目中,我必须为奥运会的一些数据建立一个数据库。
我还必须构建以下查询:
计算用户提供的特定奥运会的奖牌表。奖牌表应包含国家的国际奥委会代码,后跟金牌、银牌、铜牌和奖牌总数。它应该首先按金数排序,然后是银数,最后是铜数。
基本上,我有一张表Medals
,其中包含一些参加者在某些奥运会上获得的奖牌。
奖牌按以下方式存储:“金牌”、“银牌”、“铜牌”在color
我的表字段中Medals
。
我尝试使用以下查询:
SELECT q1.country, q1.name as "Game", q1.cntG, q2.cntS, q3.cntB FROM
(
SELECT c.countryName as country, g.name as name, count(m.idMedal) as cntG
FROM Game g
INNER JOIN Participant p
ON p.fkGame = g.idGame
INNER JOIN Country c
ON p.fkCountry = c.idCountry
INNER JOIN Medals m
ON m.fkMedalist = p.idParticipant
WHERE
g.name = "2012 Summer Olympics"
AND m.color like '%Gold%'
GROUP BY c.countryName
ORDER BY c.countryName, cntG DESC
) as q1,
(
SELECT c.countryName as country, g.name as name, count(m.idMedal) as cntS
FROM Game g
INNER JOIN Participant p
ON p.fkGame = g.idGame
INNER JOIN Country c
ON p.fkCountry = c.idCountry
INNER JOIN Medals m
ON m.fkMedalist = p.idParticipant
WHERE g.name = "2012 Summer Olympics"
AND m.color like '%Silver%'
GROUP BY c.countryName
ORDER BY c.countryName, cntS DESC
) as q2,
(
SELECT c.countryName as country, g.name as name, count(m.idMedal) as cntB
FROM Game g
INNER JOIN Participant p
ON p.fkGame = g.idGame
INNER JOIN Country c
ON p.fkCountry = c.idCountry
INNER JOIN Medals m
ON m.fkMedalist = p.idParticipant
WHERE g.name = "2012 Summer Olympics"
AND m.color like '%Bronze%'
GROUP BY c.countryName
ORDER BY c.countryName, cntB DESC
) as q3
GROUP BY q1.country
ORDER BY q1.cntG, q2.cntS, q3.cntB DESC
好吧,它给了我一个完全奇怪的结果。我知道这个查询有问题,但无法弄清楚它是什么!
希望你能帮我 :)
谢谢
注意:我暂时忽略了查询的总数(如作业中所要求的)。一旦我想出构建第一部分,我将尝试全部