需要内部连接具有相同列名的2个表中的所有列

时间:2013-10-24 18:41:02

标签: php mysql inner-join

我看到了这些问题的答案数量,但所有答案似乎都指向相同的结果。 我需要内部连接2个表,其中一列具有相同的名称。我的代码:

$sql="SELECT * FROM Bets
INNER JOIN Users ON Bets.UserID = Users.UserID
INNER JOIN Games ON Bets.GameID = Games.GameNo
WHERE GameID = '$NGnumber' ORDER BY DrawOrder";

在我看到的所有帖子中,解决方案是在具有相同名称的列上创建别名,例如:

$sql="SELECT Bets.AwayScore AS B-away FROM Bets
INNER JOIN Users ON Bets.UserID = Users.UserID
INNER JOIN Games ON Bets.GameID = Games.GameNo
WHERE GameID = '$NGnumber' ORDER BY DrawOrder";

但我有很多列要选择,如果我写它们我的所有SQL查询将永远写入!有没有办法在保留SELECT * from Bets时创建别名?

谢谢!

1 个答案:

答案 0 :(得分:2)

如果要选择表格的所有列,请使用tablename.*

SELECT b.*, b.AwayScore as bet_awayscore, g.AwayScore as game_awayscore
FROM Bets b
INNER JOIN Users u ON b.UserID = u.UserID
INNER JOIN Games g ON b.GameID = g.GameNo
WHERE b.GameID = '$NGnumber' 
ORDER BY DrawOrder