Matthias Reuter: mySQL SELECT query - syntaxfehler?

Beitrag lesen

Hallo,

ich habe echt alles versucht und schon mehrere Personen gefragt und hoffe, dass ihr mir weiterhelfen könnt.

Um Topics aus einem Forum aufzulisten, wird normalerweise die folgende QUERY benutzt:

$SQL = 'SELECT
 t.*,
 p.*,
 g.g_dohtml
  AS
  usedohtml
 FROM
  ibf_topics t
 LEFT JOIN
  ibf_posts p
  ON
  (t.tid=p.topic_id)
 LEFT JOIN
  ibf_members m
  ON
  (p.author_id=m.id)
 LEFT JOIN
  ibf_groups g
  ON
  (m.mgroup=g.g_id)
 WHERE
  '.$forums."t.approved='1'
  AND
  p.new_topic='1'
 ORDER BY
  ".$order."
 LIMIT
  $start,$limit";

Dem liegt eine Tabelle nach dem folgenden Schema zugrunde:
http://root.pc-intern.com/db_topics_screen.png

Nun möchte ich, wenn das Feld "moved_to" nicht NULL ist und die Zeichenfolge mit dem Syntax: int&int vorhanden ist, diese filtern und die Daten des entsprechenden topics laden, der in der ersten Ziffer genannt wird. (im Falle des Screenshots also den Topic 5675).

Bisher bin ich soweit gekommen:

$SQL = 'SELECT
 t.*,
 p.*,
 g.g_dohtml
  AS
  usedohtml,
 LEFT(moved_to, INSTR(moved_to,"&"))
  AS
  firstint
 FROM
  ibf_topics t
 LEFT JOIN
  ibf_topics t
  ON
  (t.tid=firstint)
 LEFT JOIN
  ibf_posts p
  ON
  (t.tid=p.topic_id)
 LEFT JOIN
  ibf_members m
  ON
  (p.author_id=m.id)
 LEFT JOIN
  ibf_groups g
  ON
  (m.mgroup=g.g_id)
 WHERE
  '.$forums.'t.approved="1"
  AND
  p.new_topic="1"
 ORDER BY
  '.$order.'
 LIMIT
  '.$start.','.$limit.'
';

Allerdings gibt es hier eine Fehlermeldung:
    IPS Driver Error
    There appears to be an error with the database.
    You can try to refresh the page by clicking here

Bestimmt ist das nur ein Syntax-Fehler - aber ich kann ihn einfach nicht finden. Leider konnte mir auch nicht die sehr ergiebige Seite http://dev.mysql.com/doc/refman/5.0/en/string-functions.html weiterhelfen. Weiß hier jemand eine Lösung?

Die Fehlermeldung kommt erst, wenn ich
 LEFT JOIN
  ibf_topics t
  ON
  (t.tid=firstint)
eingefügt habe und noch nicht bei
,
 LEFT(moved_to, INSTR(moved_to,"&"))
  AS
  firstint

Vielen Dank schonmal für eure Unterstützung!

Beste Grüße,
Matthias