Left join two values in one row

iwillieyou

Table matches has the following columns:

id, teamA, teamB

Table teams contains:

id, name

I want to output both of the teams, and managed to do it with this:

$sql1 = SELECT teams.name FROM matches LEFT JOIN teams ON matches.teamA = teams.id
$sql2 = SELECT teams.name FROM matches LEFT JOIN teams ON matches.teamb = teams.id

Then I output it with a while loop:

echo $sql1->name, $sql2->name

The problem I had when trying one query was, that the team name could only be output once per row.

How would this work with one query?

Mark Baker
SELECT team1.name as team1name, // Select the first team name from the table aliased as team1, aliasing that column as team1
       team2.name as team2name  // Select the second team name from the table aliased as team2, aliasing that column as team2
  FROM matches
  LEFT JOIN teams team1         // Alias our first join to teams on teamA as team1
    ON matches.teamA = team1.id
  LEFT JOIN teams team2         // Alias our second join to teams on teamB as team2
    ON matches.teamB = teams2.id

When you execute this query, you should get 2 team names returned, one aliased as team1name, the other as team2name; so when you retrieve these to your PHP, they will have those aliased column names

DEMO

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Mysql Left join with One row

From Dev

query for join two column and get one row per left with column that have array of right table refrenced to left table row

From Dev

MySQL left join limit to one row

From Dev

MySQL left join limit to one row

From Dev

LEFT OUTER JOIN selects just one row

From Dev

merging two left join on same table into one

From Dev

merging two left join on same table into one

From Dev

SQL JOIN with WHERE condition when two rows' values are the same and one row matches to two different rows

From Dev

left join tables, use only one row from left table

From Dev

SQL Server join two dates into one row

From Dev

One to one left join

From Dev

INNER/LEFT JOIN two tables and extend result with row

From Dev

MySQL left join first and second results and return as one row

From Dev

MySQL LEFT JOIN only one row, ordered by column without subquery

From Dev

SQL left join only returns one row instead of many

From Dev

Left Self Join return one row per 'primary'

From Dev

MySQL LEFT JOIN only one row, ordered by column without subquery

From Dev

Left Self Join return one row per 'primary'

From Dev

SQL left join only returns one row instead of many

From Dev

Return one row in a view based on a left join column value

From Dev

I'm trying to only get one row from a LEFT JOIN

From Dev

SQL LEFT JOIN without duplicating same row values

From Dev

SQL LEFT JOIN without duplicating same row values

From Dev

LEFT JOIN to newest row

From Dev

LINQ: Include clause is causing two left join when there should be one

From Dev

Left join on two Lists and maintain one property from the right with Linq

From Dev

Left Join two tables and get unmapped values as Unknown

From Dev

Left Join two tables and get unmapped values as Unknown

From Dev

repetitive values when using two left join mysql

Related Related

  1. 1

    Mysql Left join with One row

  2. 2

    query for join two column and get one row per left with column that have array of right table refrenced to left table row

  3. 3

    MySQL left join limit to one row

  4. 4

    MySQL left join limit to one row

  5. 5

    LEFT OUTER JOIN selects just one row

  6. 6

    merging two left join on same table into one

  7. 7

    merging two left join on same table into one

  8. 8

    SQL JOIN with WHERE condition when two rows' values are the same and one row matches to two different rows

  9. 9

    left join tables, use only one row from left table

  10. 10

    SQL Server join two dates into one row

  11. 11

    One to one left join

  12. 12

    INNER/LEFT JOIN two tables and extend result with row

  13. 13

    MySQL left join first and second results and return as one row

  14. 14

    MySQL LEFT JOIN only one row, ordered by column without subquery

  15. 15

    SQL left join only returns one row instead of many

  16. 16

    Left Self Join return one row per 'primary'

  17. 17

    MySQL LEFT JOIN only one row, ordered by column without subquery

  18. 18

    Left Self Join return one row per 'primary'

  19. 19

    SQL left join only returns one row instead of many

  20. 20

    Return one row in a view based on a left join column value

  21. 21

    I'm trying to only get one row from a LEFT JOIN

  22. 22

    SQL LEFT JOIN without duplicating same row values

  23. 23

    SQL LEFT JOIN without duplicating same row values

  24. 24

    LEFT JOIN to newest row

  25. 25

    LINQ: Include clause is causing two left join when there should be one

  26. 26

    Left join on two Lists and maintain one property from the right with Linq

  27. 27

    Left Join two tables and get unmapped values as Unknown

  28. 28

    Left Join two tables and get unmapped values as Unknown

  29. 29

    repetitive values when using two left join mysql

HotTag

Archive