1

我正在尝试完成以下任务-

我有 2 张足球队的桌子(不是我创建的,这是我必须使用的):

won_matches-
columns: team_id | match_name | scored_goals

lost_matches-
columns: team_id | match_name | scored_goals

teams_names-
team_id | team_name 

(我不在乎比赛名称或进球数)

我需要做的是 COUNT 每个团队在 won_matches 表中有多少条目以及在 lost_matches 表中有多少条目,然后将 lost_matches 的数量除以 won_matches 的数量,从而获得输/赢匹配率。然后,我需要为每个团队(或所有团队)提供这个比率及其团队名称。

我尝试了这样的事情,但它不能按需要工作:

SELECT b. team_name, (SELECT COUNT(team_id)
FROM won_matches [***optional; WHERE team_id=37***]) / COUNT(a.team_id)*100 AS lost_won_ratio
FROM lost_matches a 
join teams_names b on a.team_id=b.team_id
[***optional; WHERE a.team_id=37***]

将不胜感激您的建议。

4

3 回答 3

0

像这样的东西应该工作。

SELECT tn.teamID, sum(won_matches.teamID ) as WON, sum(lost_matches.teamID ) as LOST,(sum(won_matches.teamID )/sum(lost_matches.teamID )) as WLratio
From teams_names AS tn LEFT JOIN won_matches ON tn.teamID = won_matches.teamID LEFT JOIN lost_matches ON tn.teamID = lost_matches.teamID 
于 2013-07-30T18:05:55.090 回答
0

尝试这样的事情:

select team_id, Won, count(*) as Matches, sum(scored_goals) as Goals
from 
(select 1 as Won, Team_id, scored_goals from won_matches
union all
select 0 as Won, team_id, scored_goals from lost_matches) x
group by team_id, Won
于 2013-07-30T17:33:44.567 回答
0

我认为这样的事情会奏效:

select team_id,
       count(won), count(lost),
       count(won)/(count(won)+count(lost)) as 'Win ratio' from
(
select True as won, NULL as lost, won_matches.* from won_matches
union all
select NULL as won, True as lost, lost_matches.* from lost_matches
) as S group by team_id

http://sqlfiddle.com/#!2/6dbaf/2(编辑:使用加入显示团队名称)

请注意,我没有考虑可能的抽签匹配,因为我不知道它是如何存储在您的数据库中的。

编辑注意,我也用作count(won)/(count(won)+count(lost))计数比率公式。这似乎更符合逻辑。如果你坚持下去,count(lost)/count(win)你将不得不处理除以0的情况......

于 2013-07-30T17:55:22.087 回答