I'm confused using a JOIN
- Statement to select my data from my MySQL Database. Here is how the tables look like:
Table "Users"
+----+-------+----------+
| ID | Name | Password |
+----+-------+----------+
| 1 | Mike | test |
| 2 | Tony | test1 |
| 3 | Frank | test2 |
+----+-------+----------+
Table "Games"
+----+-----------+-----------+---------+---------+
| ID | Player1ID | Player2ID | ScoreP1 | ScoreP2 |
+----+-----------+-----------+---------+---------+
| 1 | 1 | 2 | 5 | 2 |
| 2 | 3 | 1 | 2 | 1 |
+----+-----------+-----------+---------+---------+
I would like to SELECT * FROM GAMES WHERE Player1ID=1 or Player2ID=1
plus the names of the users and not just their IDs. Can someone help me with that?
Join the users
table twice with different alias names for them to distinguish them
select g.*, u1.name as player1, u2.name as player2
from games g
join users u1 on u1.id = g.player1id
join users u2 on u2.id = g.player2id
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.