Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [MySQL] Zliczanie komentarzy przy pobieraniu newsa
l0ud
post
Post #1





Grupa: Zarejestrowani
Postów: 1 387
Pomógł: 273
Dołączył: 18.02.2008

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


Witam, mam taki problem:

2 tabelki mysql (news, comments) utworzone w ten sposób:

  1. CREATE TABLE news (
  2. id int UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  3. topic varchar(250) NOT NULL,
  4. contents text NOT NULL,
  5. autoformat tinyint NOT NULL,
  6. datetime char(16) NOT NULL
  7. );
  8.  
  9. CREATE TABLE comments (
  10. id int UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  11. username char(50) NOT NULL,
  12. mail char(50),
  13. type tinyint UNSIGNED NOT NULL,
  14. contents text NOT NULL,
  15. datetime char(16) NOT NULL
  16. );


I teraz chcę jednym zapytaniem pobrać newsy wraz z ilością komentarzy dla każdego. Kombinuję w ten sposób:
  1. SELECT COUNT(comments.id),news.id,news.contents,news.topic,news.datetime,news.autoformat
  2. FROM news,comments GROUP BY news.id ORDER BY id DESC


Ale nie działa :/

Proszę o pomoc (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
l0ud
post
Post #2





Grupa: Zarejestrowani
Postów: 1 387
Pomógł: 273
Dołączył: 18.02.2008

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


Heh, faktycznie, dopiero teraz zauważyłem brak powiązania pomiędzy tabelami, a miałem to zrobić na samym początku. Ok, teraz tabela comments wygląda tak:

  1. CREATE TABLE comments (
  2. id int UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  3. obj_id int UNSIGNED NOT NULL,
  4. username char(50) NOT NULL,
  5. mail char(50),
  6. type tinyint UNSIGNED NOT NULL,
  7. contents text NOT NULL,
  8. datetime char(16) NOT NULL
  9. );


Aczkolwiek nie wiem, jak wykonać owe łączenie... (IMG:http://forum.php.pl/style_emoticons/default/sad.gif) Daj proszę jakiś przykład użycia count z łączeniem tabel

Pozdrawiam
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Aktualny czas: 7.10.2025 - 11:52