1

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?

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

I will try that... thanks! Can you check my EDIT. What if want to SELECT...WHERE ? thanks
Just add the where clause to the end of this query

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.