我看到了这个问题的答案数量,但所有答案似乎都指向相同的结果。
我需要内部连接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
的同时创建别名?谢谢!
最佳答案
如果要选择表的所有列,请使用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
关于php - 需要内部联接2个表中具有相同列名的所有列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19573985/