[英]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_date
和end_date
,似乎player
一次僅是一個團隊的member
。
我們可能要包括的比較date
從match
表的start_date
和end_date
的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 )
請注意,日期比較中的一個包括等式,而另一方則不包括(因為同player
的相同行為,同一行上的start_date
看起來等於member
另一行上的end_date
。
(對於比賽date='2011-07-18'
,哪個球員是player=1
的成員?)
在這種情況下,我們還希望處理end_date
的NULL
值,該值表示一個大於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) |
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
| 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.