SQL:当我使用两个不同的 INNER JOIN 和总共 3 个表时,记录重复

问题描述 投票:0回答:1

我想同时使用2个

INNER JOIN
并加入3张桌子:
Match_Next
Match_Results
MyTest
。我的目标是通过循环和字典获取字典列表中
score_home
away_club
。当我使用第二个
INNER JOIN
时出现问题,因为score_homes在列表中重复了3或4次。

最初,当我将

Match_Next
表与
Match_Results
表一起加入时,使用
home_club
作为匹配,一切正常。事实上,例如芝加哥就有
score_home = (0, 0, 1, 2)

x = cur.execute('''SELECT Match_Next.home_club||" VS "||Match_Next.away_club,
                          Match_Next.turn, Match_Results.score_home 
                   FROM Match_Next
                   INNER JOIN Match_Results ON Match_Next.home_club = Match_Results.home_club''')

问题:是当我插入第二个

INNER JOIN
时,因为我得到的不是
score_home = (0, 0, 1, 2)
,而是类似于
score_home = (0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2)
的东西。

我想要什么结果?: 对于上面的代码,我想添加第二个

INNER JOIN
,然后我想添加
A
表的列
B
MyTest
,并打印(用于例如)芝加哥
score_home = (0, 0, 1, 2)
,而不是我使用第二个 INNER JOIN 时错误得到的重复值。所以我在这里也使用
home_club
作为匹配并写道:
INNER JOIN MyTest ON Match_Next.team_home = MyTest.team_home

x = cur.execute('''SELECT Match_Next.home_club||" VS "||Match_Next.away_club,
                          Match_Next.turn, Match_Results.score_home,
                          MyTest.A, MyTest.B
                   FROM Match_Next
                   INNER JOIN Match_Results ON Match_Next.home_club = Match_Results.home_club
                   INNER JOIN MyTest ON Match_Next.home_club = MyTest.home_club''')

Match_Next
表是这样的:

CREATE TABLE "Match_Next" (
"Id" INTEGER,
"turn" INTEGER,
"home_club" INTEGER,
"away_club" INTEGER,
"score_home" NUMERIC,
"score_away" NUMERIC,
PRIMARY KEY("Id" AUTOINCREMENT)
);

INSERT INTO Match_Next
  (`turn`, `home_club`, `away_club`, `score_home`, `score_away`)
VALUES
  ('8', 'Chicago', 'Lakers', '?', '?'),
  ('8', 'Minnesota', 'Dallas', '?', '?');

Match_Results
表是这样的:

CREATE TABLE "Match_Results" (
    "Id" INTEGER,
    "turn" INTEGER,
    "home_club" INTEGER,
    "away_club" INTEGER,
    "score_home" NUMERIC,
    "score_away" NUMERIC,
    PRIMARY KEY("Id" AUTOINCREMENT)
    );

INSERT INTO Match_Results
  (`turn`, `home_club`, `away_club`, `score_home`, `score_away`)
VALUES
  ('7', 'Chicago', 'New York', '2', '1'),
  ('7', 'Dallas', 'Indiana', '1', '0'),
  ('6', 'Milwaukee', 'Dallas', '0', '2'),
  ('6', 'Detroit', 'Chicago', '3', '2'),
  ('5', 'Chicago', 'Brooklyn', '1', '4'),
  ('5', 'Dallas', 'Memphis Gr', '2','3'),
  ('4', 'Chicago', 'Oklahoma', '0', '2'),
  ('4', 'Golden State', 'Dallas', '3', '1'),
  ('3', 'Dallas', 'Phoenix', '3', '0'),
  ('3', 'San Antonio', 'Chicago', '4', '1'),
  ('2', 'Chicago', 'Detroit', '0', '2'),
  ('2', 'Toronto', 'Dallas', '1', '3'),
  ('1', 'Dallas', 'Boston', '2', '1'),
  ('1', 'Indiana', 'Chicago', '2', '0')

MyTest
表是:

CREATE TABLE "MyTest" (
"Id" INTEGER,
"turn" INTEGER,
"home_club" INTEGER,
"away_club" INTEGER,
"score_home" NUMERIC,
"score_away" NUMERIC,
"A" NUMERIC,
"B" NUMERIC,
PRIMARY KEY("Id" AUTOINCREMENT)
);

INSERT INTO MyTest
  (`turn`, `home_club`, `away_club`, `score_home`, `score_away`, `A`, `B`)
VALUES
  ('7', 'Chicago', 'New York', '2', '1', 'example_aaa', 'example_bbb'), 
  ('7', 'Dallas', 'Indiana', '1', '0', 'example_aaa', 'example_bbb'), 
  ('6', 'Milwaukee', 'Dallas', '0', '2', 'example_aaa', 'example_bbb'), 
  ('6', 'Detroit', 'Chicago', '3', '2', 'example_aaa', 'example_bbb'), 
  ('5', 'Chicago', 'Brooklyn', '1', '4', 'example_aaa', 'example_bbb'), 
  ('5', 'Dallas', 'Memphis Gr', '2','3', 'example_aaa', 'example_bbb'), 
  ('4', 'Chicago', 'Oklahoma', '0', '2', 'example_aaa', 'example_bbb'), 
  ('4', 'Golden State', 'Dallas', '3', '1', 'example_aaa', 'example_bbb'), 
  ('3', 'Dallas', 'Phoenix', '3', '0', 'example_aaa', 'example_bbb'), 
  ('3', 'San Antonio', 'Chicago', '4', '1', 'example_aaa', 'example_bbb'), 
  ('2', 'Chicago', 'Detroit', '0', '2', 'example_aaa', 'example_bbb'), 
  ('2', 'Toronto', 'Dallas', '1', '3', 'example_aaa', 'example_bbb'), 
  ('1', 'Dallas', 'Boston', '2', '1', 'example_aaa', 'example_bbb'), 
  ('1', 'Indiana', 'Chicago', '2', '0', 'example_aaa', 'example_bbb') 
python sql python-3.x database sqlite
1个回答
0
投票

您的 Match_results 和 MyTest 表看起来非常相似。您可以通过相似的列连接这些表。
参见示例:

SELECT   Match_Next.home_club||'-'||Match_Next.away_club,
    Match_Next.turn, Match_Results.score_home,
    MyTest.A, MyTest.B
FROM Match_Next
inner JOIN Match_Results ON Match_Next.home_club = Match_Results.home_club
inner JOIN MyTest 
  ON Match_Results.home_club = MyTest.home_club 
 and Match_Results.away_club = MyTest.away_club
  ;
© www.soinside.com 2019 - 2024. All rights reserved.