我尝试使用内部联接将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.inning,
gameday.batters.name_display_first_last,
gameday.pitches.Result
FROM
gameday.atbats
Inner join
gameday.pitches on gameday.atbats.num = gameday.pitches.gameAtBatID
inner join
gameday.batters on gameday.atbats.batter = gameday.batters.ID
where gameday.atbats.gamename = "gid_2008_09_24_cinmlb_houmlb_1"
我的问题是,当我运行此查询时,击球手的结果比他们应该的多。例如,在第一局比赛中,杰特布鲁斯(atbats表中的数字1)在第一局中应该有7个投球,但是当我运行查询时,他将投掷10个投球。我做错了什么来得到这些结果。另外,我知道这些字段名称的名字很可怕,但是它们是由其他人命名的,我还没有机会改变它们。
答案 0 :(得分:2)
我敢打赌,atbats.num
和pitches.GameAtBatID
并不意味着全球唯一地识别击球,而是他们只能唯一地识别击球在给定游戏中 。因此,除了将atbats.GameName
限制为所需游戏外,您还需要指定pitches.GameName = atbats.GameName
:
SELECT gameday.atbats.inning,
gameday.batters.name_display_first_last,
gameday.pitches.Result
FROM gameday.atbats
JOIN gameday.pitches
ON gameday.atbats.GameName = gameday.pitches.GameName
AND gameday.atbats.num = gameday.pitches.GameAtBatID
JOIN batters
ON gameday.atbats.GameName = gameday.batters.GameName
AND gameday.atbats.batter = gameday.batters.ID
WHERE gameday.atbats.gamename = 'gid_2008_09_24_cinmlb_houmlb_1'
(注意:我还为AND
添加了类似的batters
,因为虽然batters.ID
的值足够大,但真正 似乎是合理的>一个独特的领域,包含它是有意义的。)
答案 1 :(得分:1)
这是事实,因为SQL工作从TOP到buttom所以当你加入前两个表时你会有
Inner join
gameday.pitches on gameday.atbats.num = gameday.pitches.gameAtBatID
您将获得这些结果
GameName GameAtBatID Result Batter
--------------------------------------------------------------------------
gid_2008_09_24_cinmlb_houmlb_1 1 Called Strike 457803
gid_2008_09_24_cinmlb_houmlb_1 1 Ball 457803
gid_2008_09_24_cinmlb_houmlb_1 1 Swinging Strike 457803
gid_2008_09_24_cinmlb_houmlb_1 1 Ball 457803
gid_2008_09_24_cinmlb_houmlb_1 1 Foul 457803
gid_2008_09_24_cinmlb_houmlb_1 1 Foul 457803
gid_2008_09_24_cinmlb_houmlb_1 1 Swinging Strike 457803
gid_2008_09_24_cinmlb_houmlb_1 2 Ball 433898
gid_2008_09_24_cinmlb_houmlb_1 2 In play, out(s) 433898
gid_2008_09_24_cinmlb_houmlb_1 3 Called Strike 458015
gid_2008_09_24_cinmlb_houmlb_1 3 Ball 458015
然后当你添加
的新连接线时inner join
gameday.batters on gameday.atbats.batter = gameday.batters.ID
您将从三个表中获得这些结果
name_display_first_last GameAtBatID Result Batter
--------------------------------------------------------------------------
Jay Bruce 1 Called Strike 457803
Jay Bruce 1 Ball 457803
Jay Bruce 1 Swinging Strike 457803
Jay Bruce 1 Ball 457803
Jay Bruce 1 Foul 457803
Jay Bruce 1 Foul 457803
Jay Bruce 1 Swinging Strike 457803
Jeff Keppinger 2 Ball 433898
Jeff Keppinger 2 In play, out(s) 433898
David Newhan 3 Called Strike 458015
David Newhan 3 Ball 458015