
Otóż mam tabele:
CREATE TABLE `news` ( `id` int(11) NOT NULL AUTO_INCREMENT, `author_id` int(11) NOT NULL DEFAULT '0', `title` varchar(12) NOT NULL DEFAULT '', `date` timestamp(14) NOT NULL, `content` text NOT NULL, PRIMARY KEY (`id`) ); CREATE TABLE `comments` ( `id` int(11) NOT NULL AUTO_INCREMENT, `news_id` int(11) NOT NULL DEFAULT '0', `author_id` int(11) NOT NULL DEFAULT '0', `title` varchar(12) NOT NULL DEFAULT '', `date` timestamp(14) NOT NULL, `content` text NOT NULL, PRIMARY KEY (`id`) ); CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `login` varchar(15) NOT NULL DEFAULT '', `password` varchar(15) NOT NULL DEFAULT '', `name` varchar(15) NOT NULL DEFAULT '', `sname` varchar(15) NOT NULL DEFAULT '', `birth` timestamp(14) NOT NULL, PRIMARY KEY (`id`) );
I chcę wyciągnąć dane : news.title, news.date, news.content, count (comments.id) oraz users.login.
Doszedłem do czegoś takiego :
SELECT x.title, x.content, x.date, y.login AS author, count( z.id ) AS count_comments FROM news AS x, users AS y, comments AS z WHERE y.id = x.author_id GROUP BY x.id ORDER BY x.id LIMIT 5
I wszystko było by ok gdyby nie to że przy każdym newsie wyświetlana jest suma wszystkich komentarzy znajdujących się w bazie :/.
Z góry dzięki za pomoc.