Problem mit Join

illuminatus26

Erfahrenes Mitglied
Hallo zusammen.

Habe folgendes Query gebastelt.

Code:
SELECT `tippspiel__tipp`.`saisonid`, `tippspiel__tipp`.`spieltagid`, `tippspiel__spiel`.`lfdnr`, `tippspiel__spiel`.`datum`, `tippspiel__spiel`.`anstoss`, `tippspiel__spiel`.`heimid` AS heim, `tippspiel__spiel`.`gastid` AS gast, `tippspiel__tipp`.`heimtore`, `tippspiel__tipp`.`gasttore`, `tippspiel__tipp`.`nutzerid`
FROM `tippspiel__tipp`
 LEFT JOIN `tippspiel__spiel` ON `tippspiel__tipp`.`spielid` = `tippspiel__spiel`.`id` 
 LEFT JOIN `tippspiel__mannschaften` ON `tippspiel__spiel`.`gastid` = `tippspiel__mannschaften`.`id` 
LEFT JOIN `tippspiel__mannschaften` AS heim ON `tippspiel__spiel`.`heimid` = `tippspiel__mannschaften`.`verein`   
LEFT JOIN `tippspiel__mannschaften` AS gast ON `tippspiel__spiel`.`gastid` = `tippspiel__mannschaften`.`verein`

Die Tabellensturktur sieht so aus:

tippspiel__tipp:
id
spielid
heimtore
gasttore
nutzerid
spieltagid
saisonid

tippspiel__spiel:
id
spieltagid
heimid
gastid
lfdnr
wochentagid
anstoss
datum
saisonid

tippspiel__mannschaften:
id
verein
stadion

Jetzt wird mir leider immer nur die heim- und gastid ausgegeben, aber nicht die Vereinsnamen......
Wo liegt der Fehler?
Bekomme auch keine Fehlermeldung.
Es werden exakt die Datensätze angezeigt, die auch vorhanden sind?
Hat jemand ne Idee?

Danke.
 
Die Namen der Vereine stehen ja offensichtlich in der Tabelle `tippspiel__mannschaften`. Dann musst Du diese auch aus dieser Tabelle holen. Du joinst diese Tabelle ja auch explizit (sogar 3 mal, ich denke 2 mal dürfte reichen).

So vielleicht:

SQL:
SELECT
  `tippspiel__tipp`.`saisonid`,
  `tippspiel__tipp`.`spieltagid`,
  `tippspiel__spiel`.`lfdnr`,
  `tippspiel__spiel`.`datum`,
  `tippspiel__spiel`.`anstoss`,
  `tippspiel__spiel`.`heimid` AS `heim`,
  `tippspiel__spiel`.`gastid` AS `gast`,
  `heim`.`verein`             AS `heimname`,
  `gast`.`verein`             AS `gastname`,
  `tippspiel__tipp`.`heimtore`,
  `tippspiel__tipp`.`gasttore`,
  `tippspiel__tipp`.`nutzerid`

FROM
            `tippspiel__tipp`
  LEFT JOIN `tippspiel__spiel`                  ON `tippspiel__tipp`.`spielid` = `tippspiel__spiel`.`id` 
  LEFT JOIN `tippspiel__mannschaften` AS `heim` ON `tippspiel__spiel`.`heimid` = `tippspiel__mannschaften`.`verein`   
  LEFT JOIN `tippspiel__mannschaften` AS `gast` ON `tippspiel__spiel`.`gastid` = `tippspiel__mannschaften`.`verein`
 
Zuletzt bearbeitet von einem Moderator:
Zurück