求和mysql中同一个表的多列

时间:2015-09-24 09:51:58

标签: php mysql join union

我正在开发体育网站。

在这个网站中,我有两个表,一个是tbl_team,它存储团队详细信息,另一个表是tbl_tournamentmatches,它存储了不同轮次的锦标赛比赛的详细信息。

在我的tbl_tournamentmatches我存储team1_idteam2_idteam1_scoreteam2_score

我的表格有这样的条目:

tbl_tournamentmatches

match_id team1_id team2_id team1_score team2_score
5        4        9        15          5
6        9        16       15          5
7        4        16       5          15
8        4        16       5          15

tbl_team

team_id team_title
4       KKR
9       RR
16      CSK

我想要结果应该是这样的: -

Team name Score
CSK        35
KKR        25
RR         20

我使用了这个查询: -

select * from 
(
   SELECT sum(team1_points) as totalpoints,t.team_title 
   from tbl_team t 
   left join tbl_tournamentmatches m 
   on t.team_id = m.team1_id 
   where tournament_id = 3 AND agegroup_id = 36 
   group by team1_id 
   union 
   SELECT sum(team2_points) as totalpoints,t.team_title 
   from tbl_team t 
   left join tbl_tournamentmatches m 
   on t.team_id = m.team2_id 
   where tournament_id = 3 AND agegroup_id = 36 
   group by team2_id
) s

但我得到的结果如下: -

KKR   25
RR    15
RR     5
CSK   35

任何帮助将不胜感激。提前谢谢。

4 个答案:

答案 0 :(得分:1)

进行连接以获得每个匹配的团队和积分,然后对结果进行总结: -

SELECT team_title, sum(team_points) as totalpoints 
FROM 
(
   SELECT team1_id AS team_id, team1_points AS team_points, t.team_title 
   FROM tbl_team t 
   LEFT JOIN tbl_tournamentmatches m 
   ON t.team_id = m.team1_id 
   WHERE tournament_id = 3 AND agegroup_id = 36 
   UNION ALL
   SELECT team2_id AS team_id, team2_points AS team_points, t.team_title 
   FROM tbl_team t 
   LEFT JOIN tbl_tournamentmatches m 
   ON t.team_id = m.team2_id 
   WHERE tournament_id = 3 AND agegroup_id = 36 
) s
GROUP BY team_id, team_title

答案 1 :(得分:1)

尝试此查询

SELECT t.team_name
    ,SUM(CASE
        WHEN t.team_id = tn.team1_id THEN tn.team1_score
        WHEN t.team_id = tn.team2_id THEN tn.team2_score
    END) AS score
FROM team t
LEFT JOIN tournament tn ON t.team_id = tn.team1_id OR t.team_id = team2_id
GROUP BY t.team_name

答案 2 :(得分:0)

使用Union all将所有团队和得分视为不同的行 然后使用Inner Join

<强>查询

select t2.team_title as TeamName, 
sum(team_score) as Score 
from 
( 
    select match_id,team1_id as team_id, team1_score as team_score 
    from tblMatch 
    union all 
    select match_id,team2_id as team_id, team2_score as team_score 
    from tblMatch 
)t1 
join tblTeam t2 
on t1.team_id = t2.team_id 
group by t2.team_id;

SQL Fiddle

答案 3 :(得分:0)

你几乎得到了它。

在您的查询中,我会看到您未在表格中包含的字段(例如agegroup_id),但您只是解决方案的一个步骤:更改您的外部“select * from”“select sum(totalpoints),team_title”并在最后添加一个组:“ group by team_title

select sum(totalpoints) as totalpoints, team_title from  
(    
    SELECT sum(team1_score) as totalpoints, t.team_title     
    from tbl_team t     
    join tbl_tournamentmatches m
      on t.team_id = m.team1_id     
    --where tournament_id = 3 AND agegroup_id = 36     
    group by team1_id     
    union     
    SELECT sum(team2_score) as totalpoints, t.team_title     
    from tbl_team t     
    join tbl_tournamentmatches m 
      on t.team_id = m.team2_id     
    --where tournament_id = 3 AND agegroup_id = 36     
    group by team2_id 
) s 
group by team_title;