2012-12-05 14 views
7

Ho 6 tavoli e io sto per fare una singola istruzione SQL:Crea query SQL singola utilizzando join .è possibile?

1)participant 
    *********** 
    +id_participant 
    +id_poste 
    +name 
    +email 

2) profile_formaion 
    **************** 
    +id_poste 
    +id_formation 

3) formation 
    ********* 
    +id_formation 
    +lable 

4) poste 
    ********* 
    +id_poste 
    +label 

5) session 
    ********* 
    +id_session 
    +id_formaion 
    +lable 

6) session_composition 
    ********* 
    +id_session 
    +id_participant 

EXAMPLE:

DATI: partecipante

1 | 2 | user1 | [email protected] 
2 | 3 | user2 | [email protected] 

DATI: profile_formation

2 | 3 
2 | 4 

DATI : formazione

1 |formation1 
2 |formation2 
3 |formation3 
4 |formation4 

DATI: poste

1 |Poste1 
2 |Poste2 
3 |Poste3 

DATI: sessione

1 |1 /* id_session 1 to id_formation 1 and id_formation=1 is formation1 */ 

DATI: session_composition

1 |2 /* id_session 1 to id_participant 2 */ 

sto provando:

SELECT 
    participant.id_participant, 
    participant.id_poste, 
    participant.name, 
    participant.email, 
    formation.lable 
FROM participant 
INNER JOIN profile_formaion ON 
    profile_formaion.id_poste = participant.id_poste 
INNER JOIN formation ON 
    formation.id_formation = profile_formaion.id_formation 

Come posso usare SQL (join) per ottenere il risultato:

DATI: risultato

1 | 2 | user1 | [email protected] | poste2|formation3 
1 | 2 | user1 | [email protected] | poste2|formation4 
2 | 3 | user2 | [email protected] | poste3|formation1 // How can we join to get it. 
+0

Sì. Come dipende dalle tue relazioni. Sono tutti richiesti o facoltativi? Se alcuni sono opzionali vuoi i record del partecipante in cui le relazioni opzionali non esistono o possono essere ignorate? – Jeroen

+0

Missione: impossibile. Codice golf? Nota, se lo metti in uno strumento di schema (Access e MySQL Workbench [gratuito] ne hai uno), può essere più facile discernere visivamente le relazioni, il che può aiutarti a vedere i join di cui hai bisogno. –

+0

@Jeroen alcuni sono opzionali. Sì, desidero registrare dal partecipante. – kn3l

risposta

2

Se il tuo non contrario all'uso sindacati, sempre si può fare questo:

select 
    participant.id_participant, 
    participant.id_poste, 
    participant.name, 
    participant.email, 
    poste.label, 
    formation.lable 
from 
    participant 
    inner join poste on participant.id_poste = poste.id_poste 
    inner join profile_formaion on participant.id_poste = profile_formaion.id_poste 
    inner join formation on profile_formaion.id_formation = formation.id_formation 

union all 

select 
    participant.id_participant, 
    participant.id_poste, 
    participant.name, 
    participant.email, 
    poste.label, 
    formation.lable 
from 
    participant 
    inner join poste on participant.id_poste = poste.id_poste 
    inner join session_composition on participant.id_participant = session_composition.id_participant 
    inner join session on session_composition.id_session = session.id_session 
    inner join formation on session.id_formaion = formation.id_formation 
Problemi correlati