Q:

How to get count of grouped rows

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);

A:

If I understood the question correctly, this should do the trick for you:
SELECT 
  count(vendor_id)
, dealers2cars.dealer_id
FROM dealers2cars JOIN car ON dealers2cars.dealer_id = car.id
GROUP BY dealer_id
HAVING COUNT(vendor_id) > 1

Output:
COUNT(VENDOR_ID)    DEALER_ID
2                   1
3                   2
2                   3

Update: This is most likely the qudery you need:
SELECT 
COUNT(DISTINCT(vendor_id)),dealer_id
FROM dealers2cars JOIN car ON dealers2cars.car_id = car.id
GROUP BY dealer_id
HAVING COUNT(DISTINCT(vendor_id)) > 1

It pulls the dealers that have cars from more than 1 unique vendor.

