Witam.
Chcę stworzyć zapytanie MySQL, które wybierze z tabeli "images" wszystkie rekordy i dodatkowo przy każdym z rekordów będzie dodatkowa kolumna, w której zliczy się ilość wszystkich komentarzy (z tabeli "comments") dla danego rekordu z "images".
Czyli wynik ma wyglądać mniej więcej tak:
id |
title |
ile_komentarzy |
1 |
obrazek 1 |
13 |
2 |
obrazek 2 |
24 |
Tabela images:
CREATE TABLE IF NOT EXISTS `images` (
`img_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(256) NOT NULL,
`title` varchar(32) NOT NULL,
`descr` varchar(2048) NOT NULL,
`datetime` datetime NOT NULL,
`onmain` datetime NOT NULL,
`author` int(11) NOT NULL,
`main` tinyint(1) NOT NULL,
PRIMARY KEY (`img_id`),
KEY `author` (`author`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=204 ;
Tabela comments:
CREATE TABLE IF NOT EXISTS `comments` (
`com_id` int(11) NOT NULL AUTO_INCREMENT,
`author` int(11) NOT NULL,
`post` int(11) NOT NULL,
`com` varchar(2048) NOT NULL,
`date` datetime NOT NULL,
PRIMARY KEY (`com_id`),
KEY `author` (`author`),
KEY `post` (`post`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=32 ;