Moin,
wie beigefügter Screenshot zeigt, geht’s mit dieser Abfrage:SELECT news.id, news.content, Count(comments.id) AS anzahl
FROM news INNER JOIN comments ON news.id = comments.news_id
GROUP BY news.id, news.content;
news.id, news.content und count(comments) in einer einzelnen query
Ich hab mal BnW’s Tabellenstruktur nachgebaut, in meiner Test-Datenbank sind 3 Nachrichten und insgesamt 4 Kommentare zu zweien davon.
Falls jmd. die Datenbankstruktur mitbenutzen möchte, hier der Dump:
[code]#
Source for table comments
DROP TABLE IF EXISTS comments
;
CREATE TABLE comments
(
id
int(10) NOT NULL AUTO_INCREMENT,
news_id
int(10) DEFAULT NULL,
content
text,
PRIMARY KEY (id
)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
Dumping data for table comments
LOCK TABLES comments
WRITE;
INSERT INTO comments
VALUES (1,1,‘Kommentar 1 zu Nachricht 1’);
INSERT INTO comments
VALUES (2,1,‘Kommentar 2 zu Nachricht 2’);
INSERT INTO comments
VALUES (3,1,‘Kommentar 3 zu Nachricht 1’);
INSERT INTO comments
VALUES (4,2,‘Nur dieser Kommentar in Nachricht 2’);
UNLOCK TABLES;
Source for table news
DROP TABLE IF EXISTS news
;
CREATE TABLE news
(
id
int(10) NOT NULL AUTO_INCREMENT,
content
text,
PRIMARY KEY (id
)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
Dumping data for table news
LOCK TABLES news
WRITE;
INSERT INTO news
VALUES (1,‘Nachricht 1’);
INSERT INTO news
VALUES (2,‘Nachricht 2’);
INSERT INTO news
VALUES (3,‘Nachricht 3’);
UNLOCK TABLES;[/code]
Viel Spass wünscht
Flynn
PS: Kann man von deinem News-System schon was sehen, oder ist’s noch “work in progress”?