2015-02-07 84 views
-1

我想比較行結果並在特定列具有相同的fightID時創建新列。比較行結果並創建新列

我的查詢:

 
Fight Fight Date Player PlayerScore 
1  2014-10-10 Kevin 8 
1  2014-10-10 Chris 4 
2  2014-09-01 Kevin 8 
2  2014-09-01 Eric 4 

我想要的結果:

 
Fight Fight Date Player1 Result1 Player2 Result2 
1  2014-10-10 Kevin 8  Chris 4 
2  2014-09-01 Kevin 8  Eric 4 

順便說一句 - 我是新來的,一般

PHP/MySQL和編程PHP

SELECT fight.fightdato, fight.fightID, players.name, 
     playersfight.playerid, playersfight.playerScore 
FROM fight 
INNER JOIN playersfight ON fight.fightID = playersfight.fightID 
INNER JOIN players ON playersfight.playerID = players.playersID 

結果

Thanks fo fo幫助我!

回答

1

您需要再次加入playerplayerfight表格。

是這樣的:在一排

  1. 結果與加入同一玩家兩次:

    SELECT fight.fightID, fight.fightdato, 
         p1.name as 'Player_1', pf1.playerid as 'PlayerID_1', pf1.playerScore as 'Result_1', 
         p2.name as 'Player_2', pf2.playerid as 'PlayerID_2', pf2.playerScore as 'Result_2'   
    FROM fight 
    JOIN playersfight pf1 ON fight.fightID = pf1.fightID 
    JOIN players p1  ON pf1.playerID = p1.playersID 
    JOIN playersfight pf2 ON fight.fightID = pf2.fightID 
    JOIN players p2  ON pf2.playerID = p2.playersID 
    WHERE p1.playersID < p2.players2 
    

    WHERE條款與條件p1.playersID < p2.players2排除兩種情況,我們不想回來了。

  2. 重複 - 例如對於身份證號碼爲12的玩家,將返回與玩家ID爲(1,2)(2,1)兩次相同的戰鬥。現在它只會返回第一個排列。
+0

2行中缺少分號。 感謝您的幫助:) – Lasse 2015-02-07 19:57:25

+0

是的,我已經修復它,謝謝。 – 2015-02-07 20:08:00