tabele:
CREATE TABLE IF NOT EXISTS `glosy` (
`k_id` bigint(20) unsigned NOT NULL,
`glos` bigint(35) DEFAULT NULL,
`wartosc` bigint(35) DEFAULT NULL,
`data_dodania` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin2;
INSERT INTO `glosy` (`k_id`, `glos`, `wartosc`, `data_dodania`) VALUES
(1, 1, NULL, '2014-10-13 22:24:23'),
(1, 1, NULL, '2014-10-13 22:24:23'),
(2, 1, NULL, '2014-10-13 22:24:48'),
(2, 1, NULL, '2014-10-13 22:24:48'),
(2, 1, NULL, '2014-10-13 22:24:55'),
(2, 1, NULL, '2014-10-13 22:24:55'),
(2, 1, NULL, '2014-10-13 22:27:20'),
(2, 1, NULL, '2014-10-13 22:27:20');
CREATE TABLE IF NOT EXISTS `ksiazki` (
`id` bigint(35) unsigned NOT NULL AUTO_INCREMENT,
`tytul` varchar(455) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`enable` int(10) DEFAULT '1',
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin2 AUTO_INCREMENT=3 ;
INSERT INTO `ksiazki` (`id`, `tytul`, `enable`) VALUES
(1, 'ksiazka 1', 1),
(2, 'ksiazka 2', 1);
i zapytanie:
SELECT ksiazki.tytul AS ksiazki ,COUNT(*) AS glosy FROM
ksiazki
LEFT JOIN glosy ON ksiazki.id = glosy.k_id
GROUP BY glosy.k_id
przetestowalem u siebie.