Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Nie działające złączenie LEFT JOIN
Forum PHP.pl > Forum > Bazy danych > MySQL
Aztech
Mam 4 tabele:
rbx_users z danymi uzytkownika
rbx_topics z wątkami forum
rbx_forums z forami
rbx_subforums z subforami

Chcę je złaczyć z sobą tak aby wydobyć informacje o imieniu osoby poprzez złączenie po jej ID o tytule subforum oraz forum poprzez złączenia odpoweidnich id z zapisanymi w wątku ID. Niestety złączenie to nie działa mi porpawnie.
Poniższe złączenie
  1. SELECT rt. * , ru.user_id, ru.username, ru.name, rsf.title AS sftitle, rf.title AS ftitle
  2. FROM rbx_topics
  3. AS rt
  4. LEFT JOIN (
  5. rbx_permissions AS rp, rbx_forums AS rf, rbx_users AS ru, rbx_subforums AS rsf
  6. ) ON ( rt.topic_id = rp.topic_id AND rt.forum_id = rf.forum_id AND rt.subforum_id = rsf.subforum_id AND rt.author_id = ru.user_id )
  7. ORDER BY rt.topic_id


wyrzuca mi NULL-e
Kod
topic_id      forum_id      subforum_id      author_id      topic_kind_id      last_post_id      active      title      app_order      posts      description      user_id      username      name      sftitle      ftitle
32  83  53  33  0  0  1  wątek testowy należący do subfor  2  0  wątek należący do subforu testowego  33  Gremster  Grzegorz Szymanski  subforum testowe  forum testowe
34  83  0  2  0  0  1  wt należący do f  3  0  tutaj wstaw opis  NULL  NULL  NULL  NULL  NULL


mimo, że osoba o id 2, która powinna się złączyć istnieje w bazie i powinan pojawić sie zamiast NULLi istnieje - czy ktos ma jakiś pomysł dlaczego tak może być?

Gdy wykonam takie zapytanie, złączenie jest ok
  1. SELECT rt. * , ru.username, ru.name, ru.user_id
  2. FROM rbx_topics
  3. AS rt
  4. LEFT JOIN (
  5. rbx_users AS ru
  6. ) ON ( rt.author_id = ru.user_id )
  7. ORDER BY rt.topic_id
DeyV
wrzuć dumpa tabel i przykładowe dane.
spenalzo
  1. ...........
  2. FROM rbx_topics
  3. AS rt
  4. LEFT JOIN (
  5. rbx_permissions AS rp, rbx_forums AS rf, rbx_users AS ru, rbx_subforums AS rsf
  6. ) ON ( ..............


Ja bym obstawiał błąd w LEFT JOIN (tabela1,tabela2), nie wiem czy to jest prawidłowe.

Ja zawsze używałem tak:
  1. SELECT t1.*
  2. FROM tabela1
  3. t1 LEFT JOIN tabela2 t2 ON(t1.id=t2.costam) LEFT JOIN tabela3 t3 ON (t2.id=t3.costam) .................................

itd itp
Aztech
@spenalzo: Po części masz rację. Doczytałem się w manualu, że
Cytat
#

Previously, a USING clause could be rewritten as an ON clause that compares corresponding columns. For example, the following two clauses were semantically identical:

a LEFT JOIN b USING (c1,c2,c3)
a LEFT JOIN b ON a.c1=b.c1 AND a.c2=b.c2 AND a.c3=b.c3

Now the two clauses no longer are quite the same:

    *

      With respect to determining which rows satisfy the join condition, both joins remain semantically identical.
    *

      With respect to determining which columns to display for SELECT * expansion, the two joins are not semantically identical. The USING join selects the coalesced value of corresponding columns, whereas the ON join selects all columns from all tables. For the preceding USING join, SELECT * selects these values:

      COALESCE(a.c1,b.c1), COALESCE(a.c2,b.c2), COALESCE(a.c3,b.c3)

      For the ON join, SELECT * selects these values:

      a.c1, a.c2, a.c3, b.c1, b.c2, b.c3

      With an inner join, COALESCE(a.c1,b.c1) is the same as either a.c1 or b.c1 because both columns will have the same value. With an outer join (such as LEFT JOIN), one of the two columns can be NULL. That column will be omitted from the result.


Działa w ten sposób:
  1. SELECT rt.*, ru.username, ru.name,
  2. rsf.title AS sftitle, rf.title AS ftitle
  3.  
  4. FROM rbx_topics
  5. AS rt
  6. LEFT JOIN rbx_users AS ru ON rt.author_id = ru.user_id
  7. LEFT JOIN rbx_subforums AS rsf ON rt.subforum_id = rsf.subforum_id
  8. LEFT JOIN rbx_forums AS rf ON rt.forum_id = rf.forum_id
  9. LEFT JOIN rbx_permissions AS rp ON rt.topic_id = rp.topic_id
  10. ORDER BY rt.topic_id


Stąd moje zdziwienie początkowe, że nie działało - a w poprzednich wersjach MysQL-a działało bezproblemowo

Mam nadzieję, że się ten post komuś przyda
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.