SQL LEFT-JOIN with 2 foreign keys and merge them into one row (MySQL)

Mango D

I have following tables

Orders:

id | address1 | address2 | state
1  | 2        | 4        | Delivered
2  | 7        | 1        | Payment

Address:

id | city
1  | New York 
2  | Paris   
4  | London 
7  | Berlin

Now I need a statement to get both cities with order status.

For example order ID 1 should output: Delivered | Paris | London

I tried the following statement:

SELECT orders.state, address.city FROM orders
  LEFT JOIN address 
    ON orders.address1 = address.id OR orders.address2 = address.id;

It obviously only outputs one address city but I want both.

Any idea how the statement should look like?

Yogesh Sharma

You can do the self join:

select o.status, ad.city, ad1.city
from Orders o 
left join Address ad on ad.id = o.address1 
left join Address ad1 on ad1.id = o.address2;

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

How to assign 2 foreign keys to one row?

From Dev

MySQL left join limit to one row

From Dev

MySQL left join limit to one row

From Dev

Attempting to Join Multiple MYSQL tables in one statement using Foreign Keys

From Dev

SQL 2 Left Join in one query with count

From Dev

SQL LEFT-JOIN on 2 fields for MySQL

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

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

SQL left join only returns one row instead of many

From Dev

Left outer join in BigQuery on multiple keys doesn't if one of them is null

From Dev

Mysql Left join not using foreign key

From Dev

SQL Left Join Foreign Key Both Tables

From Dev

SQL Join with 2 columns when one of them is NULL/Blank

From Dev

How can I count multiple foreign keys and group them in a row?

From Dev

MySQL database with two foreign keys in one table

From Dev

SQL - One to Many join with left outer join

From Dev

SQL Left Inner Join (mysql)

From Dev

SQL - left join with OR operator (MySQL)

From Dev

Left join two values in one row

From Dev

LEFT OUTER JOIN selects just one row

From Dev

Obtaining last occurance of row on MySQL LEFT JOIN

From Dev

MySQL Left Join - Not empty or first row

From Dev

PDO Sql query with join 2 tables returns one row

From Dev

MySQL LEFT JOIN is returning just one row with a where condition on the right table

From Dev

MySql LEFT JOIN returns only one NULL row from the other table

From Dev

SQL join in one row in a query

Related Related

HotTag

Archive