我已经创建了一个基于奥运会的小型数据库,用于运行一些SQL查询。我正在尝试生成一个表格,显示每个国家/地区名称以及金牌,银牌和金牌的总数。根据来自这些国家的运动员赢得铜牌。以下是我的关系模型视图的截图:
这是我想要运行的查询:
<h1>Olympics Database</h1>
<h3>Summary Information</h3>
<h4>Number of Olympic Athletes from United Kingdom</h4>
<?php
$conn = mysqli_connect('localhost', '#####', '#####') or die ('Could not connect:' . mysqli_error($conn));
echo 'Successfully Connected. <br/>';
mysqli_select_db($conn, '#####') or die('Database will not open');
echo 'Database Connected. <br/> <br/>';
$query2 = 'SELECT CountryAbbrev, (SELECT COUNT(CountryAbbrev) FROM athlete WHERE MedalID = 401 AND ath.CountryAbbrev = athlete.CountryAbbrev) AS Gold, (SELECT COUNT(CountryAbbrev) FROM athlete WHERE MedalID = 402 AND ath.CountryAbbrev = athlete.CountryAbbrev) AS Silver, (SELECT COUNT(CountryAbbrev) FROM athlete WHERE MedalID = 403 AND ath.CountryAbbrev = athlete.CountryAbbrev) AS Bronze FROM athlete AS ath';
$result2 = mysqli_query($conn, $query2) or die ('Invalid Query');
echo'<table><tr><th>Country Name</th><th>Gold</th><th>Silver</th><th>Bronze</th></tr>';
$row = mysqli_fetch_row($result2);
echo'<tr><td>' .$row[0].'</td><td>' .$row[1].'</td><td>' .$row[2].'</td><td>' .$row[3].'</td></tr>';
echo'</table>';
mysqli_close($conn);
?>
我得到的只是无效的查询。按照我的关系模型观点,任何人都可以帮我弄清楚如何在一张桌子上显示每个国家赢得的金牌,银牌和铜牌总数吗?
答案 0 :(得分:1)
由于您在表中选择了未定义的字段,原始SQL查询将返回无效查询。即athlete
表没有名为MedalName
的字段。
您可以使用以下SQL查询,通过了解每个奖牌类型的ID ,我认为它们分别是金牌,银牌和铜牌的1,2,3。
SELECT CountryAbbrev,
(SELECT COUNT(CountryAbbrev) FROM athlete WHERE MedalID = 1 AND ath.CountryAbbrev = athlete.CountryAbbrev) AS Gold,
(SELECT COUNT(CountryAbbrev) FROM athlete WHERE MedalID = 2 AND ath.CountryAbbrev = athlete.CountryAbbrev) AS Silver,
(SELECT COUNT(CountryAbbrev) FROM athlete WHERE MedalID = 3 AND ath.CountryAbbrev = athlete.CountryAbbrev) AS Bronze
FROM athlete AS ath
抱歉,它应该是COUNT
而不是SUM
。查看SUM
和COUNT
from this link
答案 1 :(得分:0)
要修复“无效查询”,您需要使用medal
表格进行左连接,将AND
更改为OR
,并使用group by
条款。此外,由于您需要奖牌计数,因此您需要将SUM
替换为COUNT
。所以你可以这样做:
SELECT CountryAbbrev as country, count(athlete.MedalID)
FROM athlete
LEFT JOIN medal on medal.MedalID = athlete.MedalID
WHERE MedalName = "Gold"
OR MedalName = "Silver"
OR MedalName = "Bronze"
GROUP BY CountryAbbrev;
此查询输出每个国家/地区的总奖牌数(#gold + #silver + #cumle):
+---------+------------------------+
| country | count(athlete.MedalID) |
+---------+------------------------+
| AU | 4 |
| US | 5 |
+---------+------------------------+
如果您想在差异列中单独打印出金,银和铜的数量,那么您可以使用以下查询:
SELECT CountryAbbrev as country,
sum(if(MedalName = "Gold", 1, 0)) as Gold,
sum(if(MedalName = "Silver", 1, 0)) as Silver,
sum(if(MedalName = "Bronze", 1, 0)) as Bronze
FROM athlete
LEFT JOIN medal on medal.MedalID = athlete.MedalID
WHERE MedalName = "Gold"
OR MedalName = "Silver"
OR MedalName = "Bronze"
GROUP BY CountryAbbrev;
此查询将输出如下内容:
+---------+------+--------+--------+
| country | Gold | Silver | Bronze |
+---------+------+--------+--------+
| AU | 1 | 2 | 1 |
| US | 2 | 1 | 2 |
+---------+------+--------+--------+
如果您还要添加总列,请尝试以下查询:
SELECT CountryAbbrev as country,
sum(if(MedalName = "Gold", 1, 0)) as Gold,
sum(if(MedalName = "Silver", 1, 0)) as Silver,
sum(if(MedalName = "Bronze", 1, 0)) as Bronze,
count(MedalName) as Total
FROM athlete
LEFT JOIN medal on medal.MedalID = athlete.MedalID
WHERE MedalName = "Gold"
OR MedalName = "Silver"
OR MedalName = "Bronze"
GROUP BY CountryAbbrev;
此查询将输出:
+---------+------+--------+--------+-------+
| country | Gold | Silver | Bronze | Total |
+---------+------+--------+--------+-------+
| AU | 1 | 2 | 1 | 4 |
| US | 2 | 1 | 2 | 5 |
+---------+------+--------+--------+-------+
我在我的机器上运行了这些查询,它们按预期工作。
答案 2 :(得分:0)
试试这个
SELECT c.CountryAbbrev,
(select count(a.MedalID) FROM athlete a WHERE c.CountryAbbrev=a.CountryAbbrev and a.MedalName = "Gold" ) as gold,
(select count(a.MedalID) FROM athlete a WHERE c.CountryAbbrev=a.CountryAbbrev and a.MedalName = "Silver" ) as Silver,
(select count(a.MedalID) FROM athlete a WHERE c.CountryAbbrev=a.CountryAbbrev and a.MedalName = "Bronze" ) as Bronze
from country as c