繁体   English   中英

同一表上的多个联接

[英]multiple joins on same tables

到目前为止,我有这个。

SELECT p1.player_id, p1.real_name, p2.player_id, p2.real_name,
    ->      m.scoreA, m.scoreB
    -> FROM matches m
    -> JOIN players p1 ON p1.player_id = m.playerA
    -> JOIN players p2 ON p2.player_id = m.playerB;
+-----------+------------------------------------+-----------+------------------------------------+--------+--------+
| player_id | real_name                          | player_id | real_name                          | scoreA | scoreB |
+-----------+------------------------------------+-----------+------------------------------------+--------+--------+
|        19 | Seong Hun Choi                     |         1 | Dong Nyoung Lee                    |      0 |      2 |
|        38 | Yun Sik Choi                       |         1 | Dong Nyoung Lee                    |      0 |      2 |
|       160 | Lei Wang                           |         1 | Dong Nyoung Lee                    |      0 |      2 |
|         3 | Seung Hyun Lee                     |         1 | Dong Nyoung Lee                    |      0 |      2 |
|        45 | Min Chul Kim                       |         1 | Dong Nyoung Lee                    |      0 |      2 |
|        23 | Sasha Hostyn                       |         1 | Dong Nyoung Lee                    |      2 |      4 |
|        52 | Juan Moreno Duran                  |         1 | Dong Nyoung Lee                    |      2 |      3 |
|        18 | Kang Dong Hyun                     |         1 | Dong Nyoung Lee                    |      2 |      1 |
|        22 | Jae Duck Lim                       |         1 | Dong Nyoung Lee                    |      1 |      2 |
|       108 | Jérémy Vansnick                  |         1 | Dong Nyoung Lee                    |      1 |      2 |

我想在不更改我的matchs table大小的情况下,从members表中加入更多列。 我尝试的所有操作似乎都改变了我的比赛表的大小。 它从46k增加到超过一百万。 到目前为止,这是我尝试过的。

SELECT p1.player_id, p1.real_name, p2.player_id, p2.real_name, 
     m.scoreA, m.scoreB, mem1.player, mem2.player
FROM matches m
JOIN players p1 ON p1.player_id = m.playerA
JOIN members mem1 ON mem1.player = p1.player_id
JOIN players p2 ON p2.player_id = m.playerB
JOIN members mem2 ON mem2.player = p2.player_id;

我以为也许我把相等的部分设置错了,所以我尝试了一下。

SELECT p1.player_id, p1.real_name, p2.player_id, p2.real_name, 
     m.scoreA, m.scoreB, mem1.player, mem2.player
FROM matches m
JOIN players p1 ON p1.player_id = m.playerA
JOIN members mem1 ON mem1.player = m.playerA
JOIN players p2 ON p2.player_id = m.playerB
JOIN members mem2 ON mem2.player = m.playerB;

我希望它完成后看起来像这样。

    +-----------+------------------------------------+-----------+------------------------------------+--------+--------+
| player_id | real_name                          | player_id | real_name                          | scoreA | scoreB |  member1     member2
+-----------+------------------------------------+-----------+------------------------------------+--------+--------+
|        19 | Seong Hun Choi                     |         1 | Dong Nyoung Lee                    |      0 |      2 |
|        38 | Yun Sik Choi                       |         1 | Dong Nyoung Lee                    |      0 |      2 |
|       160 | Lei Wang                           |         1 | Dong Nyoung Lee                    |      0 |      2 |
|         3 | Seung Hyun Lee                     |         1 | Dong Nyoung Lee                    |      0 |      2 |
|        45 | Min Chul Kim                       |         1 | Dong Nyoung Lee                    |      0 |      2 |
|        23 | Sasha Hostyn                       |         1 | Dong Nyoung Lee                    |      2 |      4 |
|        52 | Juan Moreno Duran                  |         1 | Dong Nyoung Lee                    |      2 |      3 |
|        18 | Kang Dong Hyun                     |         1 | Dong Nyoung Lee                    |      2 |      1 |
|        22 | Jae Duck Lim                       |         1 | Dong Nyoung Lee                    |      1 |      2 |
|       108 | Jérémy Vansnick                  |         1 | Dong Nyoung Lee                    |      1 |      2 |
|        53 | Igor Turchin                       |         1 | Dong Nyoung Lee                    |      1 |      2 |
|        17 | Hyun Woo Park                      |         1 | Dong Nyoung Lee                    |      0 |      2 |

我还需要使用另外两个表来重复此过程,因此非常感谢您帮助我理解如何执行此过程。


匹配表。

+----------+------------+------------+---------+---------+--------+--------+---------+
| match_id | date       | tournament | playerA | playerB | scoreA | scoreB | offline |
+----------+------------+------------+---------+---------+--------+--------+---------+
|        1 | 2012-12-04 |        799 |       4 |      55 |      1 |      3 |       0 |
|        2 | 2012-12-03 |      11921 |       2 |      41 |      2 |      0 |       0 |
|        3 | 2012-12-03 |      11921 |      21 |      41 |      0 |      2 |       0 |
|        4 | 2012-12-03 |      11921 |       3 |       2 |      2 |      1 |       0 |
|        5 | 2012-12-03 |      11921 |      41 |       2 |      1 |      2 |       0 |
|        6 | 2012-12-03 |      11921 |      21 |       3 |      1 |      2 |       0 |
|        7 | 2012-12-03 |      11924 |       1 |       8 |      2 |      1 |       1 |
|        8 | 2012-12-03 |      11924 |       1 |       8 |      2 |      3 |       1 |
|        9 | 2012-12-03 |      11924 |       8 |      19 |      3 |      2 |       1 |
|       10 | 2012-12-03 |      11924 |      19 |      12 |      2 |      1 |       1 |
+----------+------------+------------+---------+---------+--------+--------+---------+

玩家表

+-----------+-----------+-----------------+-------------+------------+-----------+
| player_id | tag       | real_name       | nationality | birthday   | game_race |
+-----------+-----------+-----------------+-------------+------------+-----------+
|         1 | Leenock   | Dong Nyoung Lee | KR          | 1995-04-01 | Z         |
|         2 | Creator   | Hyun Woo Jang   | KR          | 1996-08-19 | P         |
|         3 | Life      | Seung Hyun Lee  | KR          | 1997-01-11 | Z         |
|         4 | DongRaeGu | Soo Ho Park     | KR          | 1991-06-03 | Z         |
|         5 | PartinG   | Lee Sak Won     | KR          | 1994-08-24 | P         |
|         6 | TaeJa     | Young Suh Yoon  | KR          | 1995-01-01 | T         |
|         7 | Rain      | Yoon Jong Jung  | KR          | 1992-08-14 | P         |
|         8 | viOLet    | Dong Hwan Kim   | KR          | 1990-12-05 | Z         |
|         9 | Sniper    | Tae Hoon Kwon   | KR          | 1995-01-22 | Z         |
|        10 | Stephano  | Ilyes Satouri   | FR          | 1993-03-12 | Z         |
+-----------+-----------+-----------------+-------------+------------+-----------+

会员表

+--------+------+------------+------------+
| player | team | start_date | end_date   |
+--------+------+------------+------------+
|      1 |   88 | 2011-02-23 | 2011-07-18 |
|      1 |    4 | 2011-07-18 | NULL       |
|      2 |    3 | 2011-01-12 | NULL       |
|      3 |   65 | 2010-10-19 | 2012-07-13 |
|      3 |    1 | 2012-07-13 | NULL       |
|      4 |    7 | 2011-02-20 | NULL       |
|      5 |   88 | 2010-10-29 | 2011-07-18 |
|      5 |    1 | 2012-10-21 | 2013-01-03 |
|      5 |   12 | 2013-01-14 | NULL       |
|      6 |   65 | 2011-03-01 | 2011-05-01 |
+--------+------+------------+------------+

该查询似乎正在生成半笛卡尔积。

player表在member表中不是唯一的。 根据start_dateend_date ,似乎player一次仅是一个团队的member

我们可能要包括的比较datematch表的start_dateend_datemember表。 我们可能还想使其成为外部联接。

LEFT
JOIN `member` mem2
  ON mem2.player      = m.playerB
 AND mem2.start_date <= m.date
 AND ( mem2.end_date >  m.date OR mem2.end_date IS NULL )

请注意,日期比较中的一个包括等式,而另一方则不包括(因为同player的相同行为,同一行上的start_date看起来等于member另一行上的end_date

(对于比赛date='2011-07-18' ,哪个球员是player=1的成员?)

在这种情况下,我们还希望处理end_dateNULL值,该值表示一个大于match date的大值。

我们也想对mem1使用相同的模式。

匹配表的构造方式存在一个初始问题。 考虑以下“翻转”每个玩家并得分进入自己单行的情况。 现在,后续联接的数量立即减半:

  select
        piv, match_id, date, tournament, offline
      , case when piv = 1 then playerA 
             else playerB 
        end as Player_id
      , case when piv = 1 then scoreA 
             else scoreB 
        end as Score
  from matches
  cross join (select 1 as piv union all select 2) cj

匹配表中的10行从该查询变为20行:

| piv | match_id |                 date | tournament | offline | Player_id | Score |
|-----|----------|----------------------|------------|---------|-----------|-------|
|   1 |        1 | 2012-12-04T00:00:00Z |        799 |       0 |         4 |     1 |
|   2 |        1 | 2012-12-04T00:00:00Z |        799 |       0 |        55 |     3 |
|   1 |        2 | 2012-12-03T00:00:00Z |      11921 |       0 |         2 |     2 |
|   2 |        2 | 2012-12-03T00:00:00Z |      11921 |       0 |        41 |     0 |
|   1 |        3 | 2012-12-03T00:00:00Z |      11921 |       0 |        21 |     0 |
|   2 |        3 | 2012-12-03T00:00:00Z |      11921 |       0 |        41 |     2 |
|   1 |        4 | 2012-12-03T00:00:00Z |      11921 |       0 |         3 |     2 |
|   2 |        4 | 2012-12-03T00:00:00Z |      11921 |       0 |         2 |     1 |
|   1 |        5 | 2012-12-03T00:00:00Z |      11921 |       0 |        41 |     1 |
|   2 |        5 | 2012-12-03T00:00:00Z |      11921 |       0 |         2 |     2 |
|   1 |        6 | 2012-12-03T00:00:00Z |      11921 |       0 |        21 |     1 |
|   2 |        6 | 2012-12-03T00:00:00Z |      11921 |       0 |         3 |     2 |
|   1 |        7 | 2012-12-03T00:00:00Z |      11924 |       1 |         1 |     2 |
|   2 |        7 | 2012-12-03T00:00:00Z |      11924 |       1 |         8 |     1 |
|   1 |        8 | 2012-12-03T00:00:00Z |      11924 |       1 |         1 |     2 |
|   2 |        8 | 2012-12-03T00:00:00Z |      11924 |       1 |         8 |     3 |
|   1 |        9 | 2012-12-03T00:00:00Z |      11924 |       1 |         8 |     3 |
|   2 |        9 | 2012-12-03T00:00:00Z |      11924 |       1 |        19 |     2 |
|   1 |       10 | 2012-12-03T00:00:00Z |      11924 |       1 |        19 |     2 |
|   2 |       10 | 2012-12-03T00:00:00Z |      11924 |       1 |        12 |     1 |

因此,以此为起点,连接玩家数据非常简单,尽管对于members表也很简单,但我们确实要确保仅从该表中获取其“当前”行,因此将今天的每个团队开始/结束日期之间的日期(如果结束日期为NULL,则使用今天的日期)。

select
*
from (
      select
            piv, match_id, date, tournament, offline
          , case when piv = 1 then playerA 
                 else playerB 
            end as Player_id
          , case when piv = 1 then scoreA 
                 else scoreB 
            end as Score
      from matches
      cross join (select 1 as piv union all select 2) cj
     ) g
left join Players p on g.Player_id = p.Player_id
left join Members m on g.Player_id = m.player 
                    and curdate() between m.start_date and coalesce(m.end_date,curdate())

这不会在20行中产生进一步的增加:

| piv | match_id |                 date | tournament | offline | Player_id | Score | player_id |       tag |       real_name | nationality |             birthday | game_race | player |   team |           start_date | end_date |
|-----|----------|----------------------|------------|---------|-----------|-------|-----------|-----------|-----------------|-------------|----------------------|-----------|--------|--------|----------------------|----------|
|   1 |        7 | 2012-12-03T00:00:00Z |      11924 |       1 |         1 |     2 |         1 |   Leenock | Dong Nyoung Lee |          KR | 1995-04-01T00:00:00Z |         Z |      1 |      4 | 2011-07-18T00:00:00Z |   (null) |
|   1 |        8 | 2012-12-03T00:00:00Z |      11924 |       1 |         1 |     2 |         1 |   Leenock | Dong Nyoung Lee |          KR | 1995-04-01T00:00:00Z |         Z |      1 |      4 | 2011-07-18T00:00:00Z |   (null) |
|   1 |        2 | 2012-12-03T00:00:00Z |      11921 |       0 |         2 |     2 |         2 |   Creator |   Hyun Woo Jang |          KR | 1996-08-19T00:00:00Z |         P |      2 |      3 | 2011-01-12T00:00:00Z |   (null) |
|   2 |        4 | 2012-12-03T00:00:00Z |      11921 |       0 |         2 |     1 |         2 |   Creator |   Hyun Woo Jang |          KR | 1996-08-19T00:00:00Z |         P |      2 |      3 | 2011-01-12T00:00:00Z |   (null) |
|   2 |        5 | 2012-12-03T00:00:00Z |      11921 |       0 |         2 |     2 |         2 |   Creator |   Hyun Woo Jang |          KR | 1996-08-19T00:00:00Z |         P |      2 |      3 | 2011-01-12T00:00:00Z |   (null) |
|   1 |        4 | 2012-12-03T00:00:00Z |      11921 |       0 |         3 |     2 |         3 |      Life |  Seung Hyun Lee |          KR | 1997-01-11T00:00:00Z |         Z |      3 |      1 | 2012-07-13T00:00:00Z |   (null) |
|   2 |        6 | 2012-12-03T00:00:00Z |      11921 |       0 |         3 |     2 |         3 |      Life |  Seung Hyun Lee |          KR | 1997-01-11T00:00:00Z |         Z |      3 |      1 | 2012-07-13T00:00:00Z |   (null) |
|   1 |        1 | 2012-12-04T00:00:00Z |        799 |       0 |         4 |     1 |         4 | DongRaeGu |     Soo Ho Park |          KR | 1991-06-03T00:00:00Z |         Z |      4 |      7 | 2011-02-20T00:00:00Z |   (null) |
|   2 |        7 | 2012-12-03T00:00:00Z |      11924 |       1 |         8 |     1 |         8 |    viOLet |   Dong Hwan Kim |          KR | 1990-12-05T00:00:00Z |         Z | (null) | (null) |               (null) |   (null) |
|   2 |        8 | 2012-12-03T00:00:00Z |      11924 |       1 |         8 |     3 |         8 |    viOLet |   Dong Hwan Kim |          KR | 1990-12-05T00:00:00Z |         Z | (null) | (null) |               (null) |   (null) |
|   1 |        9 | 2012-12-03T00:00:00Z |      11924 |       1 |         8 |     3 |         8 |    viOLet |   Dong Hwan Kim |          KR | 1990-12-05T00:00:00Z |         Z | (null) | (null) |               (null) |   (null) |
|   2 |        1 | 2012-12-04T00:00:00Z |        799 |       0 |        55 |     3 |    (null) |    (null) |          (null) |      (null) |               (null) |    (null) | (null) | (null) |               (null) |   (null) |
|   2 |        2 | 2012-12-03T00:00:00Z |      11921 |       0 |        41 |     0 |    (null) |    (null) |          (null) |      (null) |               (null) |    (null) | (null) | (null) |               (null) |   (null) |
|   1 |        3 | 2012-12-03T00:00:00Z |      11921 |       0 |        21 |     0 |    (null) |    (null) |          (null) |      (null) |               (null) |    (null) | (null) | (null) |               (null) |   (null) |
|   2 |        3 | 2012-12-03T00:00:00Z |      11921 |       0 |        41 |     2 |    (null) |    (null) |          (null) |      (null) |               (null) |    (null) | (null) | (null) |               (null) |   (null) |
|   1 |        5 | 2012-12-03T00:00:00Z |      11921 |       0 |        41 |     1 |    (null) |    (null) |          (null) |      (null) |               (null) |    (null) | (null) | (null) |               (null) |   (null) |
|   1 |        6 | 2012-12-03T00:00:00Z |      11921 |       0 |        21 |     1 |    (null) |    (null) |          (null) |      (null) |               (null) |    (null) | (null) | (null) |               (null) |   (null) |
|   2 |        9 | 2012-12-03T00:00:00Z |      11924 |       1 |        19 |     2 |    (null) |    (null) |          (null) |      (null) |               (null) |    (null) | (null) | (null) |               (null) |   (null) |
|   1 |       10 | 2012-12-03T00:00:00Z |      11924 |       1 |        19 |     2 |    (null) |    (null) |          (null) |      (null) |               (null) |    (null) | (null) | (null) |               (null) |   (null) |
|   2 |       10 | 2012-12-03T00:00:00Z |      11924 |       1 |        12 |     1 |    (null) |    (null) |          (null) |      (null) |               (null) |    (null) | (null) | (null) |               (null) |   (null) |

SQLfiddle演示

nb:要返回到该信息的透视图,请执行以下操作:

select
      match_id, match_date, tournament
    , max(case when piv = 1 then real_name end) PlayerAreal_name
    , max(case when piv = 1 then player_Id end) PlayerA
    , max(case when piv = 1 then player_Id end) ScoreA

    , max(case when piv = 2 then real_name end) PlayerBreal_name
    , max(case when piv = 2 then player_Id end) PlayerB
    , max(case when piv = 2 then player_Id end) Scoreb
from (
       the big query above goes here
     )
group by 
  match_id, match_date, tournament

DEMO

| match_id |           match_date | tournament | PlayerAreal_name | PlayerA | ScoreA | PlayerBreal_name | PlayerB | Scoreb |
|----------|----------------------|------------|------------------|---------|--------|------------------|---------|--------|
|        1 | 2012-12-04T00:00:00Z |        799 |      Soo Ho Park |       4 |      4 |           (null) |      55 |     55 |
|        2 | 2012-12-03T00:00:00Z |      11921 |    Hyun Woo Jang |       2 |      2 |           (null) |      41 |     41 |
|        3 | 2012-12-03T00:00:00Z |      11921 |           (null) |      21 |     21 |           (null) |      41 |     41 |
|        4 | 2012-12-03T00:00:00Z |      11921 |   Seung Hyun Lee |       3 |      3 |    Hyun Woo Jang |       2 |      2 |
|        5 | 2012-12-03T00:00:00Z |      11921 |           (null) |      41 |     41 |    Hyun Woo Jang |       2 |      2 |
|        6 | 2012-12-03T00:00:00Z |      11921 |           (null) |      21 |     21 |   Seung Hyun Lee |       3 |      3 |
|        7 | 2012-12-03T00:00:00Z |      11924 |  Dong Nyoung Lee |       1 |      1 |    Dong Hwan Kim |       8 |      8 |
|        8 | 2012-12-03T00:00:00Z |      11924 |  Dong Nyoung Lee |       1 |      1 |    Dong Hwan Kim |       8 |      8 |
|        9 | 2012-12-03T00:00:00Z |      11924 |    Dong Hwan Kim |       8 |      8 |           (null) |      19 |     19 |
|       10 | 2012-12-03T00:00:00Z |      11924 |           (null) |      19 |     19 |           (null) |      12 |     12 |

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM