1

在我的数据库项目中,我必须为奥运会的一些数据建立一个数据库。

我还必须构建以下查询:

计算用户提供的特定奥运会的奖牌表。奖牌表应包含国家的国际奥委会代码,后跟金牌、银牌、铜牌和奖牌总数。它应该首先按金数排序,然后是银数,最后是铜数。

基本上,我有一张表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

好吧,它给了我一个完全奇怪的结果。我知道这个查询有问题,但无法弄清楚它是什么!

希望你能帮我 :)

谢谢

注意:我暂时忽略了查询的总数(如作业中所要求的)。一旦我想出构建第一部分,我将尝试全部

4

3 回答 3

3

数据不是问题——在 q1、q2 和 q3 之间存在隐式笛卡尔连接这一事实一个问题。尝试:

SELECT c.countryName as country, 
       count(case m.idMedal when 'Gold medal' then 1 end) as cntG,
       count(case m.idMedal when 'Silver medal' then 1 end) as cntS,
       count(case m.idMedal when 'Bronze medal' then 1 end) 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" 
GROUP BY c.countryName
ORDER BY cntG DESC, cntS DESC, cntB DESC
于 2013-06-02T19:54:04.997 回答
2

对不起,但你的查询是一个真正的混乱。您不能以这种方式简单地加入 3 个子查询。

但是,您可以在一个查询中获得所需的内容。我会给你伪代码,把细节留给你:)

SELECT
    [countryName],
    SUM(color like '%Gold%') as total_gold,
    SUM(color like '%Silver%') as total_silver,
    SUM(color like '%Bronze%') as total_bronze,
    COUNT(*) as total
FROM Medals
INNER JOIN Participant (...)
INNER JOIN Country (...)
INNER JOIN Game (...)
WHERE (...)
GROUP BY [countryName]
ORDER BY total_gold DESC, total_silver DESC, total_bronze DESC;
于 2013-06-02T19:52:46.180 回答
1

有很多方法可以修复甚至改进您的查询,其中之一是添加

 ...
 WHERE q1.country = q2.country 
   AND q1.country = q3.country

(就在之前GROUP BY q1.country)。

另一种解决方案是 JOIN q1, q2 和 q3 ON country

于 2013-06-02T19:55:59.333 回答