Ciao a tutti,
nel db ho una tabella, ServErog (servizi erogati) che si lega ad altre 4 tabelle ServA, ServB, ServC, ServD (tipi di servizi disponibili difformi tra loro e non aggregabili in una tabella unica) incrociando il servtype (riferimento al tipo di servizio, ossia a quale tabella guardare) e l'id del singolo servizio
la struttura (semplificata):
ServErog
mysql> select * from ServErog
+----+-------+----------+------+------+------+---------+-------+------+
| idSE | servtype | type_id |
+----+-------+----------+------+------+------+---------+-------+------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 4 | 1 |
| 4 | 3 | 1 |
| 5 | 1 | 2 |
+----+-------+----------+------+------+------+---------+-------+------+
ServA
mysql> select * from ServA
+----+-------+----------+------+------+------+---------+-------+------+
| idSA | service_code | type |
+----+-------+----------+------+------+------+---------+-------+------+
| 1 | codice bla | 1 |
| 2 | codice ecc | 1 |
| 3 | bla bla | 1 |
+----+-------+----------+------+------+------+---------+-------+------+
ServB
mysql> select * from ServB
+----+-------+----------+------+------+------+---------+-------+------+
| idSB | service_code | type |
+----+-------+----------+------+------+------+---------+-------+------+
| 1 | codice bla | 2 |
| 2 | codice ecc | 2 |
| 3 | bla bla | 2 |
+----+-------+----------+------+------+------+---------+-------+------+
ServC
mysql> select * from ServC
+----+-------+----------+------+------+------+---------+-------+------+
| idSC | service_code | type |
+----+-------+----------+------+------+------+---------+-------+------+
| 1 | codice bla | 3 |
| 2 | codice ecc | 3 |
| 3 | bla bla | 3 |
+----+-------+----------+------+------+------+---------+-------+------+
ServD
mysql> select * from ServD
+----+-------+----------+------+------+------+---------+-------+------+
| idSA | service_code | type |
+----+-------+----------+------+------+------+---------+-------+------+
| 1 | codice bla | 4 |
| 2 | codice ecc | 4 |
| 3 | bla bla | 4 |
+----+-------+----------+------+------+------+---------+-------+------+
Select
ServErog.idSE,
ServErog.servtype,
ServErog.typeid,
ServA.idSA,
ServA.type,
ServB.idSB,
ServB.type,
Serv.idSA,
Serv.type,
ServD.idSA,
ServD.type
From
ServErog
Left Join
ServA On ServErog.servtype = ServA.type And ServA.idSA = ServErog.typeid
Left Join
ServB On ServErog.servtype = ServB.type And ServB.idSB = ServErog.typeid
Left Join
ServC On ServErog.servtype = ServC.type And ServC.idSC = ServErog.typeid
Left Join
ServD On ServErog.servtype = ServD.type And ServD.idSD = ServErog.typeid
Order By
ServErog.idSE
+----+-------+----------+------+------+------+---------+-------+------+
| idSE | servtype | type_id | idSA | idSB | idSC | idSD|
+----+-------+----------+------+------+------+---------+-------+------+
| 1 | 1 | 1 | 1 | null | null | null |
| 2 | 2 | 1 | null | 1 | null | null |
| 3 | 4 | 1 | null | null | null | 1 |
| 4 | 3 | 1 | null | null | 1 | null |
| 5 | 1 | 2 | 2 | null | null | null |
+----+-------+----------+------+------+------+---------+-------+------+
Ottengo così tutti i campi relazionati a ServErog.
Perfetto!
Ma se volessi creare una query che mi mostra tutti i valori presenti nelle tabelle ServA, ServB, ServC, ServD NON COLLEGATI a ServErog come posso fare?
In pratica è come se dovessere invertire/negare la Left Join precedente.
Ho provato con right join, con idSE is null ma non ci salto fuori
per capirci, nel caso specifico dovrebbe mostrare questo:
+----+-------+----------+------+------+------+---------+-------+------+
| idSA | idSB | idSC | idSD|
+----+-------+----------+------+------+------+---------+-------+------+
| 3 | null | null | null |
| null | 2 | null | null |
| null | 3 | null | null |
| null | null |2 | null |
|null | null | 3 | null |
| null | null |null | 2 |
| null | null | null | 3 |
+----+-------+----------+------+------+------+---------+-------+------+
Se poi ritenete che sia da rivedere il concetto di relazione tra le tabelle sono ben accette osservazioni/consigli
Grazie

Rispondi quotando
