我有3个表我正在尝试加入内连接,但由于某种原因,它们会导致一堆重复的结果。我一直在试图弄清楚我的头发出了什么问题。以下是我正在使用的表格:
Table:gameday.atbats
GameName Inning num b s o Batter Pitcher Result
-----------------------------------------------------------------------------------------
gid_2008_09_24_cinmlb_houmlb_1 1 1 2 3 1 457803 150116 Jay Bruce strikes out swinging.
gid_2008_09_24_cinmlb_houmlb_1 1 2 1 0 2 433898 150116 Jeff Keppinger lines out to right fielder Hunter Pence.
gid_2008_09_24_cinmlb_houmlb_1 1 3 3 1 2 458015 150116 Joey Votto singles on a line drive to right fielder Hunter Pence.
gid_2008_09_24_cinmlb_houmlb_1 1 4 2 3 3 429665 150116 Edwin Encarnacion called out on strikes.
gid_2008_09_24_cinmlb_houmlb_1 1 5 1 2 0 430565 459371 Kazuo Matsui singles on a line drive to right fielder Jay Bruce.
-----------------------------------------------------------------------------------------
Table: Gameday.pitches
GameName GameAtBatID Result
------------------------------------------------------
gid_2008_09_24_cinmlb_houmlb_1 1 Called Strike
gid_2008_09_24_cinmlb_houmlb_1 1 Ball
gid_2008_09_24_cinmlb_houmlb_1 1 Swinging Strike
gid_2008_09_24_cinmlb_houmlb_1 1 Ball
gid_2008_09_24_cinmlb_houmlb_1 1 Foul
gid_2008_09_24_cinmlb_houmlb_1 1 Foul
gid_2008_09_24_cinmlb_houmlb_1 1 Swinging Strike
gid_2008_09_24_cinmlb_houmlb_1 2 Ball
gid_2008_09_24_cinmlb_houmlb_1 2 In play, out(s)
gid_2008_09_24_cinmlb_houmlb_1 3 Called Strike
gid_2008_09_24_cinmlb_houmlb_1 3 Ball
--------------------------------------------------------
Table:batters
GameName id name_display_first_last
----------------------------------------------------------------------------------
gid_2008_09_24_cinmlb_houmlb_1 407783 Geoff Geary
gid_2008_09_24_cinmlb_houmlb_1 209315 David Newhan
gid_2008_09_24_cinmlb_houmlb_1 115629 LaTroy Hawkins
gid_2008_09_24_cinmlb_houmlb_1 113889 Darin Erstad
gid_2008_09_24_cinmlb_houmlb_1 457803 Jay Bruce
gid_2008_09_24_cinmlb_houmlb_1 433898 Jeff Keppinger
gid_2008_09_24_cinmlb_houmlb_1 458015 Joey Votto
gid_2008_09_24_cinmlb_houmlb_1 429665 Edwin Encarnacion
---------------------------------------------------------------------------
我正在使用的代码如下:
SELECT gameday.atbats.event
, gameday.atbats.inning
, gameday.batters.name_display_first_last
, gameday.pitchers.name_display_first_last
, gameday.pitches.result
FROM gameday.atbats
inner join gameday.pitches on gameday.atbats.num = gameday.pitches.gameAtBatID
and gameday.pitches.gamename=gameday.atbats.gamename
inner join gameday.batters on gameday.atbats.batter = gameday.batters.ID
AND gameday.atbats.gamename = gameday.batters.gameName
where gameday.atbats.gamename = "gid_2008_09_24_cinmlb_houmlb_1"
当前代码导致每个玩家的结果加倍,例如,如果游戏中的第一个击球手击出3个球场,则当前代码将输出该击球手在第一局中以6个球场击出两次。我加入了游戏ID和每张桌子的ID,但似乎仍然存在某种问题
答案 0 :(得分:1)
没有冒犯,但我不喜欢"使用不同的"回答是因为它只会掩盖问题。在这种情况下我做的是在SELECT COUNT(*)
正上方插入FROM
行,然后逐步查看结果,一次添加一行;即,SELECT COUNT(*) FROM A
,然后A JOIN B
,然后是A JOIN B JOIN C
等,以检测有问题的JOIN
。
答案 1 :(得分:0)
在你的查询中在最后一行,你已经用游戏名称写了where条件,所以游戏名称是唯一的,如果不是那么你会得到多个答案,所以更好的方法在gameday.atbats的条件下使用主键表