Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 同一表上的多个联接_Mysql_Sql - Fatal编程技术网

Mysql 同一表上的多个联接

Mysql 同一表上的多个联接,mysql,sql,Mysql,Sql,到目前为止,我有这个 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; +-------

到目前为止,我有这个

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 |
我想从我的
成员
表中加入更多的列,而不更改匹配项的大小
。我尝试的每件事似乎都改变了我的火柴桌的大小。从4.6万到100多万。这就是我迄今为止所尝试的

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
表中不是唯一的。根据
开始日期
结束日期
,一名
球员一次只能是一支球队的
成员

我们可能希望将
匹配表中的
日期
成员表中的
开始日期
结束日期
进行比较。我们可能还想让它成为一个外部连接

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 )
左
加入'member'成员2
在mem2.player=m.playerB上
和mem2.start_date m.date或mem2.end_date为空)
请注意,其中一个日期比较包含一个相等值,而另一方则没有(因为对于相同的
玩家
,行上的
开始日期似乎等于
成员
中另一行上的
结束日期)

(对于日期为2011-07-18'
的比赛,哪个队是
player=1
的成员?)

我们还希望处理
end_date
中的
NULL
值,在这种情况下,该值表示大于
匹配的
日期的大值


我们希望对<代码> 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
matches表中的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表也很简单,但我们确实希望确保只从该表中获取他们的“当前”行,因此我们将今天的日期放在每个团队的开始/结束日期之间(如果结束日期为空,则使用今天的日期)

这不会进一步增加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) |

注意:要恢复到信息的透视视图,请执行以下操作:

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


你能在这里写模式吗?不要显示多表查询的结果,而是显示每个表的一些数据。包括每个表的DDL也会有帮助。这就是你的意思吗?
player|id | real|u name | player|id | real|u name | scoreA | scoreB | member1
对不起,我不知道你所说的模式是什么意思。只显示一个f每个表中的ew行(带标题)。这将对我们有所帮助。请将其包含在问题中(此处不作为注释,也不作为图片)不是多表中的行queries@Used_By_Already完成。如果您还需要什么,请告诉我。我如何更好地发现这些问题?我一天中的大部分时间都在关注这个问题,但无法解决它。我想我可以在设置连接后解决日期问题。当我构建这样的查询时,我会将主或唯一的k选择列表开头的每个表的Y列,并按相同的主键列排序。避免重复的关键是确定关系的保证基数…我需要知道它是否保证为一对一,或一对零或一,或一对零或多,或多对多。您能帮帮忙吗我明白吗?我不明白piv是什么,它来自哪里,以及它是如何产生的。我想记录从47k翻倍到92k的原因是你把每个玩家放在自己的线上,对吗?交叉连接是如何工作的?我从来没有使用过它们。交叉连接的缩写是
cj
(选择1作为piv union all select 2)cj
交叉连接将行无条件地放在一起(实际上它是行的倍数)。是cj=CROSS JOIN=YES。“piv”只是我从稀薄的空气中提取的一个列名,是piv piv union all select 2的缩写。它是由
选择1作为piv union all select 2“生成”的,尝试一下,它只输出2行。“记录从47k翻倍到92k的原因是你把每个球员都放在了自己的线上,对吗?”=是的
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
| 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 |