2012-04-18 108 views
1

我不知道什麼是做這個SQL兩列相同的連接使用不同的值

爭取最好的辦法

Fighter_ID1 | Fighter_ID2 

FIGHTERS

ID | Fighter 

OUTPUT

Fighter1 | Fighter2 

我想加入該列的表FIGHTERS用表FIGHT兩列的fighter ..

它可以是一個愚蠢的問題,但我不知道如何以正確的方式做到這一點

對不起我的英語和感謝

+0

您想要將結果一個接一個或在兩個單獨的列中結果相同嗎? – rcdmk 2012-04-18 22:29:24

+0

de輸出有2列 – leinho 2012-04-18 22:32:51

回答

2
SELECT F1.Fighter as Fighter1, 
     F2.Fighter as Fighter2 
FROM FIGHT 
     INNER JOIN FIGHTERS as F1 ON FIGHT.Fighter_ID1 = F1.ID 
     INNER JOIN FIGTHERS as F2 ON FIGHT.Fighter_ID2 = F2.ID 
1

看起來像你需要加入在桌子上的兩倍。

SELECT fs1.Fighter as Fighter1, fs2.Fighter as Figther2 
FROM Fight f 
INNER JOIN Fighters fs1 
    ON f.Fighter_ID1 = fs1.id 
INNER JOIN Fighters fs2 
    ON f.Fighter_ID2 = fs2.id 
1

怎麼樣

select f1.name, f2.name -- pick any column you like 
    from FIGHT f -- from here we join both fighters by id 
    join FIGHTER f1 on f.fighter_id1 = f1.id 
    join FIGHTER f2 on f.fighter_id2 = f2.id 
1

因爲你將有2名戰士每次吵架,你會想你的別名表。我直接在這裏打字,所以我不能保證它是100%準確的,但它應該是這個樣子:

SELECT * 
FROM FIGHT 
    INNER JOIN FIGHTERS AS F1 ON F1.ID = Fighter_ID1 
    INNER JOIN FIGHTERS AS F2 ON F2.ID = Fighter_ID2 

然後你可以使用F1.Fighter和F2.Fighter爲您的字段名戰鬥機。

相關問題