简体   繁体   中英

Need to inner join all colums from 2 tables with same column names

I saw numbers of answers for this questions but all answers seem to point to the same result. I need to inner join 2 tables with one column that has the same name in both. My code:

$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";

In all posts i've seen, the solution is to create an alias on the column that has the same name, for instance:

$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";

But I have a lot of columns to select, if I write them all my SQL query will take forever to write! Is there a way to create an alias while keeping SELECT * from Bets ?

Thanks!

If you want to select all columns of a table then use 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM