Mysql 为什么我的SQL查询将结果加倍
我想把两张桌子连接起来。完成的查询如下所示:Mysql 为什么我的SQL查询将结果加倍,mysql,Mysql,我想把两张桌子连接起来。完成的查询如下所示: select cast(M.Rank as signed) as Rank, O.Country, O.Continent, M.Gold, M.Silver, M.Bronze, M.Total from ( select @rownum := @rownum + 1 as RankStd, CASE WHEN (@Gold=T.Gold and @Silve
select
cast(M.Rank as signed) as Rank,
O.Country,
O.Continent,
M.Gold,
M.Silver,
M.Bronze,
M.Total
from
(
select @rownum := @rownum + 1 as RankStd,
CASE
WHEN (@Gold=T.Gold and @Silver=T.Silver and @Bronze=T.Bronze)
THEN @rank := @rank
ELSE @rank := @rownum
END as Rank,
(@Gold:=T.Gold) Gold,
(@Silver:=T.Silver) Silver,
(@Bronze:=T.Bronze) Bronze,
T.Total,
T.City,
T.Season
from
(
select
sum(Gold) as Gold,
sum(Silver) as Silver,
sum(Bronze) as Bronze,
sum(Total) as Total,
City,
Season
from
(
select
City,
Season,
case when Medal ='gold' then 1 else 0 end as Gold,
case when Medal ='silver' then 1 else 0 end as Silver,
case when Medal ='bronze' then 1 else 0 end as Bronze,
1 as Total
from MedalResults
where Country = 'ITA'
and Season='Summer'
) a
group by City
) T
CROSS JOIN (SELECT @rownum := 0,
@Gold := 0,
@Silver := 0,
@Bronze := 0,
@rank := 1) v
order by Gold desc, Silver DESC, Bronze DESC
)M
join OrgCountry O
on O.City = M.City and O.Season=M.Season
order by Rank
M表给我一个具有这种结构的表(我只写第一行):
OrgCountry表的结构确实是这样的(我只写有意义的行来让您理解,显然在1996年到1984年之间还有其他条目,但对于本例,我将其删掉):
如果我像上面那样在它们之间进行连接,结果会加倍,如下所示:
Rank Country Continent Gold Silver Bronze Total
-------------------------------------------------------------
1 United States Americas 26 18 24 68
1 United States Americas 26 18 24 68
2 Great Britain Europe 19 23 21 63
2 Great Britain Europe 19 23 21 63
2 Great Britain Europe 19 23 21 63
3 Greece Europe 17 17 14 48
3 Greece Europe 17 17 14 48
3 Greece Europe 17 17 14 48
为什么会这样?我想要的只是对国家进行汇总,并得到:
Rank Gold Silver Bronze Total Country Season
-----------------------------------------------------------
1 39 28 36 113 United States Summer
2 19 23 21 63 Great Britain Summer
3 17 17 14 48 Greece Summer
4 14 6 5 25 Netherlands Summer
5 13 10 13 36 Italy Summer
6 13 10 12 35 Australia Summer
等等。
请注意,第一行已更改,因为亚特兰大和洛杉矶是美国的城市 您的问题的核心如下。您的sqlfiddle不能充分代表问题,无法进一步回答:
SELECT y.country
, x.medal
, COUNT(*) subtotal
FROM medalresults x
JOIN (SELECT DISTINCT city, country FROM orgcountry) y
ON y.city = x.city
WHERE x.country = 'ITA'
GROUP
BY country
, medal;
…或者可能
SELECT y.country
, SUM(CASE WHEN medal = 'gold' THEN 1 ELSE 0 END) gold
, SUM(CASE WHEN medal = 'silver' THEN 1 ELSE 0 END) silver
, SUM(CASE WHEN medal = 'bronze' THEN 1 ELSE 0 END) bronze
FROM medalresults x
JOIN (SELECT DISTINCT city, country FROM orgcountry) y
ON y.city = x.city
WHERE x.country = 'ITA'
GROUP
BY country;
您的问题的核心如下。您的sqlfiddle不能充分代表问题,无法进一步回答:
SELECT y.country
, x.medal
, COUNT(*) subtotal
FROM medalresults x
JOIN (SELECT DISTINCT city, country FROM orgcountry) y
ON y.city = x.city
WHERE x.country = 'ITA'
GROUP
BY country
, medal;
…或者可能
SELECT y.country
, SUM(CASE WHEN medal = 'gold' THEN 1 ELSE 0 END) gold
, SUM(CASE WHEN medal = 'silver' THEN 1 ELSE 0 END) silver
, SUM(CASE WHEN medal = 'bronze' THEN 1 ELSE 0 END) bronze
FROM medalresults x
JOIN (SELECT DISTINCT city, country FROM orgcountry) y
ON y.city = x.city
WHERE x.country = 'ITA'
GROUP
BY country;
看到了吗?我添加了这一页:那么,你是想知道意大利在每个主办国获得了多少枚金牌、银牌和铜牌吗?草莓:是的,我看到我添加了这一页:那么,你是想知道意大利在每个主办国获得了多少枚金牌、银牌和铜牌吗?草莓:是的,我是