我正在写一个曲棍球统计数据库。我正在尝试计算玩家的总积分。总分是1st_assists,2nd_assists和goals_for表中的总和。最初我有以下代码工作时,我有完全填充表(IE每个玩家至少有一个第一助手)。现在我只有一个游戏,结果集不包括goal_for信息,因为有助攻的玩家没有目标。
我需要全外连接功能,我已经读过MySQL中没有这个功能。我也读过,可以通过左联接和右联接的联合来模拟完全联接。鉴于此查询已经存在的复杂性,我无法弄清楚如何做到这一点。
这是查询:
select p.player_first_name, p.player_last_name, count1, count2, (count1+ifnull(count2,0)) as total_count
from
(select player_id, count(*) count1
from(select * from 1st_assists
union
select * from 2nd_assists) as tem
join players on tem.fk_player_id=players.player_id
group by fk_player_id
order by count(*) desc) q1
left join
(select player_id, count(*) count2
from goals_for
join shots_for on goals_for.fk_shot_for_id=shots_for.shot_for_id
join players on shots_for.fk_player_id=players.player_id
group by player_id) q2
ON q1.player_id=q2.player_id
left join players p ON q1.player_id=p.player_id
order by (count1+count2) desc;
结果如下:
player_first_name player_last_name count1 count2 total_count
Ray Bourque 1 NULL 1
Dany Heatley 1 NULL 1
Mike Bossy 1 NULL 1
此游戏中还有其他3名玩家得分,这些玩家不会出现在上述结果中。如何模拟我需要的完整外连接?这就是我需要的,不是吗?
这里是表格详细信息和期望的结果:
1st_assists:
1st_assist_id
fk_goal_for_id
fk_player_id
2nd_assists:
2nd_assist_id
fk_goal_for_id
fk_player_id
goals_for:
goal_for_id
fk_shot_for_id
game_winner
time
shots_for:
shot_for_id
fk_game_id
fk_player_id
players:
player_id
player_first_name
player_last_name
desired query results
player_first_name player_last_name count1 count2 total_count
Ray Bourque 1 NULL 1
Dany Heatley 1 NULL 1
Mike Bossy 1 NULL 1
Wayne Gretzky NULL 1 1
Paul Coffey NULL 1 1
Bobby Orr NULL 1 1
我真的只对total_count值感兴趣,这是1st_assists,2nd_assists和goals_for的总和。此查询仅基于第一个游戏。会有很多游戏。