I'm trying to get the town (Localidad) from referees(ArbPrin, ArbAux, Anotador, Crono, Op24). I make the query and the result isn't correct. Per example if I only have ArbPrin and Anotador it returns the town of ArbPrin for all the referees. If ArbPrin is from Malaga and Anotador is from Torremolinos, the result is Malaga for all the referees.
This is the query:
SELECT L1.Nombre AS'LocPrin', L2.Nombre AS'LocAux', L3.Nombre AS'LocAn', L4.Nombre AS'LocCro', L5.Nombre AS'LocOp' FROM PARTIDO INNER JOIN EQUIPO_ARBITRAL A1 ON PARTIDO.ArbPrin=A1.Codigo_arbitro INNER JOIN EQUIPO_ARBITRAL A2 ON PARTIDO.ArbPrin=A2.Codigo_arbitro INNER JOIN EQUIPO_ARBITRAL A3 ON PARTIDO.ArbPrin=A3.Codigo_arbitro INNER JOIN EQUIPO_ARBITRAL A4 ON PARTIDO.ArbPrin=A4.Codigo_arbitro INNER JOIN EQUIPO_ARBITRAL A5 ON PARTIDO.ArbPrin=A5.Codigo_arbitro INNER JOIN LOCALIDAD L1 ON A1.Cod_localidad=L1.Codigo_localidad INNER JOIN LOCALIDAD L2 ON A2.Cod_localidad=L2.Codigo_localidad INNER JOIN LOCALIDAD L3 ON A3.Cod_localidad=L3.Codigo_localidad INNER JOIN LOCALIDAD L4 ON A4.Cod_localidad=L4.Codigo_localidad INNER JOIN LOCALIDAD L5 ON A5.Cod_localidad=L5.Codigo_localidad; Here an example of the result. If I have the follows referees:
ArbPrin: Malaga, ArbAux: Torremolinos, Anotador: Benalmadena
The result is the next:
LocPrin LocAux LocAn LocCro LocOp ----------------------------------------------- Malaga Malaga Malaga Malaga Malaga The result I want is the next:
LocPrin LocAux LocAn LocCro LocOp -------------------------------------------------------- Malaga Torremolinos Benalmadena
PARTIDO. You were always joining on the first field which in your fiddle contains 1, so it is logical you get the same name every where. I Joined once on the first field, then on the second field, then on the 3rd field.