Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: left join
Forum PHP.pl > Forum > Bazy danych > MySQL
deniol13
witam, mam takie zapytanie
  1. SELECT f.*, t.* FROM forums f
  2. LEFT JOIN topics t
  3. WHERE f.forum_id = t.forum_id
  4. ORDER BY t.topic_start_date DESC
  5. LIMIT 0, 1


efekt w pma
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE f.forum_id = t.forum_id
ORDER BY t.topic_start_date DESC
LIMIT 0, 1' at line 3
Mchl
LEFT (RIGHT) JOIN wymaga warunku złączenia w ON

Kod
SELECT f.*, t.* FROM forums f
LEFT JOIN topics t
ON f.forum_id = t.forum_id
ORDER BY t.topic_start_date DESC
LIMIT 0, 1
deniol13
a jak moge ograniczyc ilosc joinow ?
bo chce by mi pobralo forum i do kazdego forum ostatni temat ale jak mam dwa tematy w forum to pobiera mi forum x2 a nie dam LIMIT 0,1 (usunalem z wczesniejszego sql) bo mam kilka for
Mchl
Kod
SELECT f.*, t.* FROM forums f
LEFT JOIN (
  SELECT * FROM topics AS t
  CROSS JOIN (
    SELECT forum_id, MAX(topic_start_date) AS topic_start_date
    FROM topics
    GROUP BY forum_id
  ) ON (forum_id, topic_start_date)
) AS t
ON f.forum_id = t.forum_id
ORDER BY t.topic_start_date DESC
LIMIT 0, 1
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.