简单的MySQL加入

时间:2012-05-31 22:49:37

标签: mysql

这适用于player1name,但如何解决player2name?

当我在这里时,如果您知道任何有用的视觉查询构建器,请告诉我;)


SELECT 
_game.player1,
_user.username AS player1name,
_game.player2,
_user.username AS player2name,
FROM _game
LEFT JOIN _user ON _game.player1 = _user.id

4 个答案:

答案 0 :(得分:1)

SELECT player1,
_user.username AS player1name,
 player2,
_user2.username AS player2name
FROM _game
LEFT JOIN _user ON (_headsup.player1 = _user.id)
LEFT JOIN _user _user2 ON (_headsup.player2 = _user2.id)

答案 1 :(得分:1)

我没有运行ATM的mySQL,但它应该像......

SELECT 
player1,
u1.username AS player1name,
 player2,
u2.username AS player2name,
FROM _game
INNER JOIN _user u1 ON _headsup.player1 = u1.id
INNER JOIN _user u2 ON _headsup.player2 = u2.id

即。联接上的别名,使用select中的别名。

然而,我对_headsup的来源感到茫然!

答案 2 :(得分:1)

SELECT 
   player1,
   usr1.username AS player1name,
   player2,
   usr2.username AS player2name,
FROM _game
LEFT JOIN _user usr1 ON usr1.id= _game.player1
LEFT JOIN _user usr2 ON usr2.id= _game.player2

答案 3 :(得分:0)

当您加入_game ON _user时,我认为您必须指定在这两个表上加入哪些字段。您指定了该加入,但随后尝试加入_headsup_user

不确定player1player2是什么,但由于您没有说明它们来自哪个表格,因此可能会出现模糊字段。

首先在_game加入_user,然后在_user上与_headsup进行第二次加入

FROM _game
LEFT JOIN _user ON _game.someField = _user.someField
JOIN _headsup ON _user.id = _headsup.player1