Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [MySQL] Złożone zapytanie
Fifi209
post
Post #1





Grupa: Zarejestrowani
Postów: 4 655
Pomógł: 556
Dołączył: 17.03.2009
Skąd: Katowice

Ostrzeżenie: (0%)
-----


Witam, od dłuższego czasu męczę się ze złożonym zapytaniem...

Mam w mysql następujące tabele:

users:
id, name, password

news:
id, author_id, subject, content, time

comments:
id, news_id, author_id, subject, content, time

Potrzebuję wyciągnąć tak:
`news`.`subject`, `news`.`content`, `news`.`time`
`users`.`name`
I ilość komentarzy dla danego news'a.

Ja wyskrobałem coś takiego:
  1. SELECT
  2. `news`.`subject`,
  3. `news`.`content`,
  4. `news`.`time`,
  5. `users`.`name`,
  6. count(`comments`.`id`) AS `count`
  7. FROM
  8. `news`, `users`, `comments`
  9. WHERE `news`.`author_id` = `users`.`id` AND `comments`.`news_id` = `news`.`id`
  10. GROUP BY `news`.`id`
  11. ORDER BY `news`.`time` DESC LIMIT 5


Lecz jeżeli nie ma komentarzy to nie wyświetla...
Nie wiem jak to ugryźć ;/
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 7)
nospor
post
Post #2





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




bo comments nalezy połączyc przez LEFT JOIN a nie FROM
Go to the top of the page
+Quote Post
Fifi209
post
Post #3





Grupa: Zarejestrowani
Postów: 4 655
Pomógł: 556
Dołączył: 17.03.2009
Skąd: Katowice

Ostrzeżenie: (0%)
-----


Ja wymodziłem coś takiego...

  1. SELECT
  2. `news`.`subject`,
  3. `news`.`content`,
  4. `news`.`time`,
  5. `users`.`name`,
  6. (SELECT count(`comments`.`id`) FROM `comments` WHERE `comments`.`news_id` = `news`.`id`) AS `count`
  7. FROM
  8. `news`, `users`, `comments`
  9. WHERE `news`.`author_id` = `users`.`id`
  10. GROUP BY `news`.`id`
  11. ORDER BY `news`.`time` DESC LIMIT 5


Co do joinów, szczerze mówiąc nie korzystałem więc nie potrafię.
Pytanko, czy z joinami będzie szybciej pobierał dane (przy większych ilościach) ?
Czy mógłbyś mi pokazać przykład?
Go to the top of the page
+Quote Post
nospor
post
Post #4





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




wszystko masz tu opisane
http://dev.mysql.com/doc/refman/5.0/en/join.html
Go to the top of the page
+Quote Post
marcio
post
Post #5





Grupa: Zarejestrowani
Postów: 2 291
Pomógł: 156
Dołączył: 23.09.2007
Skąd: ITALY-MILAN

Ostrzeżenie: (10%)
X----


  1.  
  2. SELECT
  3. `news`.`subject`,
  4. `news`.`content`,
  5. `news`.`time`,
  6. `users`.`name`,
  7. count(`comments`.`id`) AS `count`
  8. FROM
  9. `news`, `users`
  10.  
  11. LEFT JOIN comments ON (news.id = comments.news_id)
  12. WHERE `news`.`author_id` = `users`.`id`
  13. GROUP BY `news`.`id`
  14. ORDER BY `news`.`time` DESC LIMIT 5
  15.  
  16.  


Go to the top of the page
+Quote Post
Fifi209
post
Post #6





Grupa: Zarejestrowani
Postów: 4 655
Pomógł: 556
Dołączył: 17.03.2009
Skąd: Katowice

Ostrzeżenie: (0%)
-----


Kombinuję z tym LEFT JOIN, efekt podobny jak to co podał kolega. Mysql twierdzi, że:
Kod
MySQL zwrócił komunikat: Dokumentacja
#1054 - Nieznana kolumna 'news.id' w  on clause
Go to the top of the page
+Quote Post
nospor
post
Post #7





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




nie:
`news`, `users`
a:
(`news`, `users`)

poza tym users tez mozesz dac jako left join
Go to the top of the page
+Quote Post
Fifi209
post
Post #8





Grupa: Zarejestrowani
Postów: 4 655
Pomógł: 556
Dołączył: 17.03.2009
Skąd: Katowice

Ostrzeżenie: (0%)
-----


Już prawie...
  1. SELECT
  2. `news`.`subject`,
  3. `news`.`content`,
  4. `news`.`time`,
  5. `users`.`name`,
  6. count(`comments`.`id`) AS `count`
  7. FROM `news`
  8. LEFT JOIN `users` ON(`news`.`author_id` = `users`.`id`)
  9. LEFT JOIN `comments` ON(`news`.`id` = `comments`.`news_id`)
  10. GROUP BY `news`.`id`

Jednak źle pokazuje mi ilość komentarzy.

@edit
Mój głupi błąd...
Miało być
LEFT JOIN `comments` ON(`news`.`id` = `comments`.`news_id`)

Dziękuję za pomoc.

Ten post edytował fifi209 2.11.2009, 22:00:27
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 22.08.2025 - 12:49