2015-05-31 65 views
3

表1連接兩個表,但沒有得到任何結果

Move1  Move2 
Fire  Ice   
Grass  Fire 
Ice   Grass   

表2

Move  Power 
Fire  40 
Grass  30 
Ice   20 

我有這兩個表,但是當我運行在WAMPServer這個SQL語句,我沒有得到任何行返回:

SELECT * 
FROM Table1, Table2 
    INNER JOIN Table1 as Hello ON Hello.move1 = Table2.move 
    INNER JOIN Table1 as Meow ON Meow.move2 = Table2.move 

這兩個表都在同一個數據庫下,所以我看不到我的錯誤。我希望它會返回類似...

表1

Move1  Move2  Move  Power  Move  Power 
Fire  Ice  Fire  40  Ice  20 
Grass  Fire  Grass 30  Fire  40 
Ice   Grass  Ice  20  Grass 30 

我有什麼錯?

回答

2

這應該工作:

select 
    m.move1, 
    m.move2, 
    m1.move, 
    m1.power, 
    m2.move, 
    m2.power 
from 
    table1 m 
    join table2 m1 on m1.move = m.move1 
    join table2 m2 on m2.move = m.move2 

可以在SQL Fiddle

1

測試此你的代碼是錯誤的幾種方法,你應該學習如何使用SQL連接(This是一個很好的,基本解說)

至於解決您的問題,試試這個:

SELECT * 
FROM table1 INNER JOIN table2 t2_m1 
    ON table1.move1 = t2_m1.move INNER JOIN t2_m2 
    ON table1.move2 = t2_m2.move 
相關問題