I need to get dealers ids, which have multiple car's vendors. By example tables, I need to get id 3. I'm stuck on this query:
SELECT COUNT(car.id), dealers2cars.dealer_id, car.vendor_id FROM car, dealers2cars
WHERE
dealers2cars.`car_id` = car.id
GROUP BY car.vendor_id, dealers2cars.`dealer_id`
But it get wrong result (all dealers and count of their cars grouped by vendor).
count dealer_id vendor_id
2,1,1
1,3,1
3,2,2
1,3,2
This is example database tables:
CREATE TABLE `car` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
`vendor_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*Data for the table `car` */
insert into `car`(`id`,`title`,`vendor_id`) values
(1,'F1',1),
(2,'480 Italia',1),
(3,'New Beatle',2),
(4,'Scirocco',2),
(5,'Golf',2);
/*Table structure for table `dealers2cars` */
CREATE TABLE `dealers2cars` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`dealer_id` int(11) DEFAULT NULL,
`car_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*Data for the table `dealers2cars` */
insert into `dealers2cars`(`id`,`dealer_id`,`car_id`) values
(1,1,1),
(2,1,2),
(3,2,3),
(4,2,4),
(5,2,5),
(6,3,1),
(7,3,5);