Hello,
I’m going crazy with this query, I have 3 tables (Users, Cities and Images) I want to show the users grouped by cities, where the user’s image and data are shown, also the city with the image of the city must be shown , An example:
City1 (image of the city), Name, etc.
User 1: (user image), Name, etc.
User 2: (user’s image), Name, etc.
User 3: (user image), Name, etc.
User 4: (user image), Name, etc.
User 5: (user’s image), Name, etc.
User 6: (user image), Name, etc.City1 (image of the city), Name, etc.
User 1: (user image), Name, etc.
User 2: (user’s image), Name, etc.
User 3: (user image), Name, etc.
User 4: (user image), Name, etc.
User 5: (user’s image), Name, etc.
User 6: (user image), Name, etc.City1 (image of the city), Name, etc.
User 1: (user image), Name, etc.
User 2: (user’s image), Name, etc.
User 3: (user image), Name, etc.
User 4: (user image), Name, etc.
User 5: (user’s image), Name, etc.
User 6: (user image), Name, etc.City1 (image of the city), Name, etc.
User 1: (user image), Name, etc.
User 2: (user’s image), Name, etc.
User 3: (user image), Name, etc.
User 4: (user image), Name, etc.
User 5: (user’s image), Name, etc.
User 6: (user image), Name, etc.
Usuarios table:
id | nombre | idFoto | idCiudad
Ciudades Table:
id | nombre | idFoto
Imagenes table
id | imagen
The city and user images are in the same table: Images
Controller:
SELECT c.id, p.nombre, c.idFoto, u.id, a.nombre, a.idFoto
FROM usuarios a
LEFT JOIN imagenes i ON u.idFoto = i.id
LEFT JOIN ciudades e ON e.idFoto = i.id
LEFT JOIN ciudades c ON u.idCiudad = c.id
WHERE NOT isnull( c.nombre )
ORDER BY c.nombre ASC
The query would be something similar to this, is that okay? How do I do it? I do not know how to do it