mok
Goto Top

N zu M Tabelle so Joinen das alle Werte zu sehen sind

Hallo, ich bin echt am verzweifeln ich habe 3 Tabellen und möchte diese so verbinden das ich alle möglichen Datensatzkombinationen angezeigt bekomme egal ob in der N:M Tabelle beziehungen eingetragen sind. Könnt ihr mir helfen?

tab1:
- id
- bezeichnung

tab2 (n:m):
- tab1_id
- tab3_id
- status

tab3:
- id
- bezeichnung


tab1 inhalte:
1 - tab1wert1
2 - tab1wert2
3 - tab1wert3

tab2 inhalte:
1 - 1 - good
2 - 1 - bad

tab3 inhalte:
1 - tab3wert1
2 - tab3wert2
3 - tab3wert3


Ergebniss der Abfrage sollte dann wie folgt sein:

tab1_bezeichnung, tab2_bezeichnung, tab3_status

tab1wert1- tab3wert1 - good
tab1wert1- tab3wert2 - NULL
tab1wert1- tab3wert3 - NULL
tab1wert2- tab3wert1 - bad
tab1wert2- tab3wert2 - NULL
tab1wert2- tab3wert3 - NULL
tab1wert3- tab3wert1 - NULL
tab1wert3- tab3wert2 - NULL
tab1wert3- tab3wert3 - NULL

Ich hoffe das ich es verständlich erklären konnte.

Gruß mok

Content-Key: 217269

Url: https://administrator.de/contentid/217269

Ausgedruckt am: 29.03.2024 um 06:03 Uhr

Mitglied: bytecounter
bytecounter 19.09.2013 um 10:41:58 Uhr
Goto Top
Hallo,

hast Du Dir schon mal JOIN angeschaut? Aber solange Du uns nicht verrätst, mit welchem Datenbanksystem Du arbeitest, wird man Dir auch nicht gezielt helfen können.

vg
Bytecounter
Mitglied: moK
moK 19.09.2013 um 23:38:57 Uhr
Goto Top
Ups.. tut mir leid, MySQL 5.1.22

Da ich JOINen muss is mir klar, aber wie das ich auch NULL Werte angezeigt bekomme?

Ich habs nur so hinbekommen das ich alle bezüge angezeigt bekomme aus von denen zu denen schon einer besteht.


SELECT t1.id AS t1_id, t3.id AS t3_id, t2.id AS t3_id, t3.status
FROM tab1 t1
LEFT JOIN tab2 t2 ON t1.id = t2.tab1_id OR t2.tab1_id IS NULL
RIGHT JOIN tab3 t3 ON t2.tab3_id = t3.id OR t2.tab3_id IS NULL;

In diesem beispiel ist das Ergebniss:

tab1wert1- tab3wert1 - good
tab1wert1- tab3wert2 - NULL
tab1wert1- tab3wert3 - NULL
tab1wert2- tab3wert1 - bad
tab1wert2- tab3wert2 - NULL
tab1wert2- tab3wert3 - NULL
> tab1wert3- tab3wert1 - NULL <-- dieser eintrag ist nicht da drinne aber ich will ihn haben.
tab1wert3- tab3wert2 - NULL
tab1wert3- tab3wert3 - NULL

Mein Kopf platzt schon, mir fällt auch nix mehr dazu ein wonach man googlen könnte. Kann doch nicht sein das nur ich so ne Verknüpfung machen möchte.
face-sad
Mitglied: Biber
Biber 20.09.2013 aktualisiert um 00:52:04 Uhr
Goto Top
Moin moK,

das Statement sollte sinngemäß so aussehen:
SELECT t1xt3.tab1bez ,  t1xt3.tab3bez , tab2.Status
FROM (
     Select tab1.id as tab1_id, tab1.bezeichnung as tab1bez,
                tab3.id as tab3_id, tab3.bezeichnung as tab3bez
 from tab1, tab3) t1xt3
      left join tab2 ON t1xt3.tab1_id = tab2.tab1_id and  t1xt3.tab3_id = tab2.tab3_id
order by t1xt3.tab1_id,  t1xt3.tab3_id ;

Ausgabe:
tab1bez       tab3bez        Status
tab1wert1	tab3wert1	good
tab1wert1	tab3wert2	
tab1wert1	tab3wert3	
tab1wert2	tab3wert1	bad
tab1wert2	tab3wert2	
tab1wert2	tab3wert3	
tab1wert3	tab3wert1	
tab1wert3	tab3wert2	
tab1wert3	tab3wert3	

Grüße
Biber
Mitglied: moK
moK 20.09.2013 um 01:00:42 Uhr
Goto Top
DAAANKE, sehr genial... im grunde genommen is es klar... man denkt manchmal einfach zu kompliziert face-sad