MySQL左边连接后的语句

时间:2015-09-19 07:05:51

标签: mysql join where-clause

我正在写一个曲棍球统计数据库。我有必要计算在进球时有多少球员在冰上。

我已经设置了以下sqlfiddle: http://sqlfiddle.com/#!9/ed8fa

我遇到的问题是如何过滤结果,以便只有在常规力量下得分的目标(击球类型“reg”)或在得分短缺时得分的目标(击球类型“sh”)计入此总数。

我需要在输出中列出所有玩家,即使他们的数字为零。在找到球员“加上评级”后,我将计算每个球员的“减去评分”并减去两个。一旦我弄清楚如何准确地找到“加上评级”,我应该能够弄清楚如何做其余的事情。

对于sqlfiddle中的给定数据,所需的输出应为:

player_id   Plus_count  
20          1  
21          0  
22          2  
23          1  
24          1  
25          0  
26          1  
27          1  
28          1  
29          0  
30          0  
31          2  
32          1  
33          0 

我无法弄清楚联接是如何工作的,这样我就可以在哪里过滤掉所有“pp”的目标。

感谢您提供的任何指示......

添加我尝试过的内容。这是来自我的dev db,所以与sqlfiddle略有不同:
SELECT players.player_id as pp_id, COALESCE(plus_players.count, 0) as Plus_Count FROM players LEFT JOIN (SELECT plus_players.fk_player_id, COUNT(*) as count FROM plus_players GROUP BY plus_players.fk_player_id) plus_players ON plus_players.fk_player_id = players.player_id left join (select * from shots_for where regular_powerplay_shorthanded = 'reg' or regular_powerplay_shorthanded = 'sh') sf on sf.fk_player_id = players.player_id union SELECT players.player_id as pp_id, COALESCE(plus_players.count, 0) as Plus_Count FROM players LEFT JOIN (SELECT plus_players.fk_player_id, COUNT(*) as count FROM plus_players GROUP BY plus_players.fk_player_id) plus_players ON plus_players.fk_player_id = players.player_id right join (select * from shots_for where regular_powerplay_shorthanded = 'reg' or regular_powerplay_shorthanded = 'sh') sf2 on sf2.fk_player_id = players.player_id left join goals_for on goals_for.fk_shot_for_id = sf2.shot_for_id where regular_powerplay_shorthanded = 'reg' or regular_powerplay_shorthanded = 'sh'

你可以看到我上面的各种尝试,用它来消除“pp”目标。

0 个答案:

没有答案