MySQL - Select multiple rows from one table whose IDs are stored in another table

Mike_NotGuilty

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?

juergen d

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

select multiple column from one table and insert into another as rows

From Dev

Taking ids from two rows of one MySQL table and entering those values in one row of another MySQL table using PHP

From Dev

Insert multiple rows from select into another table

From Dev

how to select one row from one table and multiple rows from other table using joins in mysql,

From Dev

mysql: select rows from another table as columns

From Dev

mysql - move rows from one table to another

From Dev

MYSQL - UPDATE multiple rows from another table

From Dev

insert multiple rows mysql from another table

From Dev

MYSQL select count of multiple ids occurring in another table

From Dev

Select Multiple Ids from a table

From Dev

Link one row of table to multiple rows in another table in MySQL

From Dev

MySQL query - single row in one table with multiple rows in another table

From Dev

Select rows from a table if one of them matches with another table

From Dev

MySQL project design - conditionally select from one table based on rows from another select query

From Dev

MySQL - Exclude all rows from one table if match on another table

From Dev

Mysql Trigger, Copy All Rows From One Table Into Another Table

From Dev

MySQL select * from one table based on stock from another table

From Dev

Update MYSQL Table from multiple rows in another table

From Dev

PHP + MySQL - one table holding reference to IDs from multiple tables

From Dev

MYSQL - Select all rows in a table where it's ids in another table as string field

From Dev

MYSQL: Select entries in relation to multiple rows of another table

From Dev

How to SELECT none matching rows from one table to another?

From Dev

Subtract rows of one table from another table

From Dev

JQuery: How to drag multiple rows from one table to another?

From Dev

Update multiple rows in one table with differing values from another

From Dev

Insert multiple rows with single a query from one table into another in Oracle

From Dev

JQuery: How to drag multiple rows from one table to another?

From Dev

MySQL select row with two matching joined rows from another table

From Dev

Efficiently deleting rows from one table where not matching another [MySQL]

Related Related

  1. 1

    select multiple column from one table and insert into another as rows

  2. 2

    Taking ids from two rows of one MySQL table and entering those values in one row of another MySQL table using PHP

  3. 3

    Insert multiple rows from select into another table

  4. 4

    how to select one row from one table and multiple rows from other table using joins in mysql,

  5. 5

    mysql: select rows from another table as columns

  6. 6

    mysql - move rows from one table to another

  7. 7

    MYSQL - UPDATE multiple rows from another table

  8. 8

    insert multiple rows mysql from another table

  9. 9

    MYSQL select count of multiple ids occurring in another table

  10. 10

    Select Multiple Ids from a table

  11. 11

    Link one row of table to multiple rows in another table in MySQL

  12. 12

    MySQL query - single row in one table with multiple rows in another table

  13. 13

    Select rows from a table if one of them matches with another table

  14. 14

    MySQL project design - conditionally select from one table based on rows from another select query

  15. 15

    MySQL - Exclude all rows from one table if match on another table

  16. 16

    Mysql Trigger, Copy All Rows From One Table Into Another Table

  17. 17

    MySQL select * from one table based on stock from another table

  18. 18

    Update MYSQL Table from multiple rows in another table

  19. 19

    PHP + MySQL - one table holding reference to IDs from multiple tables

  20. 20

    MYSQL - Select all rows in a table where it's ids in another table as string field

  21. 21

    MYSQL: Select entries in relation to multiple rows of another table

  22. 22

    How to SELECT none matching rows from one table to another?

  23. 23

    Subtract rows of one table from another table

  24. 24

    JQuery: How to drag multiple rows from one table to another?

  25. 25

    Update multiple rows in one table with differing values from another

  26. 26

    Insert multiple rows with single a query from one table into another in Oracle

  27. 27

    JQuery: How to drag multiple rows from one table to another?

  28. 28

    MySQL select row with two matching joined rows from another table

  29. 29

    Efficiently deleting rows from one table where not matching another [MySQL]

HotTag

Archive