Santana Santana - 5 months ago 13
MySQL Question

Mysql query: JOINS

I have the following tables:

Transfers Table:

ID | storagefrom | storageto
1 | 2 | 3


Storages Table:

ID | Name
2 | Kitchen
3 | Main


I want to get the following result:

Storage1 | Storage2
Kitchen | Main


This is my query:

SELECT storages.name as Storage1, storages.name as Storage2
FROM transfers
LEFT JOIN storages ON storages.id = transfers.storagefrom
LEFT JOIN storages ON storages.id = transfers.storageto


Any tips?

Answer

Try

SELECT T.ID,
       S1.Name Storage1,
       S2.name Storage2 
FROM transfers T
LEFT JOIN storages S1 ON S1.ID = T.storagefrom
LEFT JOIN storages S2 ON S2.ID = T.storageto

Refer : SQL Fiddle

Comments