Ho 3 tabelle strutturate così
Il mio intento è quello di restituire con una query tutti gli eventi associati ad un dato utente, in cui però invece degli id delle categorie e sottocategorie ci siano i rispettivi nomi.codice:EVENTS ------------------------------------------------------------- | id | user_id | category | subcategory | created_at | .... | |-----------------------------------------------------------| | 1 | 1 | 1 | 1 | 2015-05-10 | .... | | 2 | 1 | 2 | 3 | 2015-05-12 | .... | | 3 | 1 | 3 | | 2015-05-15 | .... | | 4 | 1 | 1 | 1 | 2015-05-17 | .... | ------------------------------------------------------------- CATEGORIES SUBCATEGORIES --------------- --------------- | id | name | | id | name | |----|--------| |----|--------| | 1 | sport | | 1 | calcio | | 2 | musica | | 2 | basket | | 3 | mostre | | 3 | rock | --------------- ---------------
Lo faccio in questo modo:
Tutto questo funziona per gli eventi con id 1, 2 e 4, ma ovviamente non per il 3, il quale non ha un id per la sottocategoria, che purtroppo è opzionale, quindi recupero 3 eventi invece di 4.codice:select events.id as id, events.title as title, events.created_at as created_at, categories.name as category, subcategories.name as subcategory from events inner join categories on events.category = categories.id inner join subcategories on events.subcategory = subcategories.id where events.user_id = 1
Come posso fare per recuperare tutti gli eventi ed avere correttamente associati i nomi di categorie e sottocategorie?

Rispondi quotando
