加入两个表但没有结果

时间:2015-05-31 07:25:27

标签: mysql sql-server wampserver

表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 个答案:

答案 0 :(得分: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 :(得分:2)

你的代码在几个方面是错误的,你应该学习如何使用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