SELECT `a`.*
FROM `a`
JOIN (
SELECT `a_id`, COUNT(*) AS `count_ab`
FROM `a_to_b`
GROUP BY `a_id`
) AS `x` ON `x`.`a_id` = `a`.`id`
JOIN (
SELECT COUNT(*) AS `count_b`
FROM `b`
) AS `b` ON `b`.`count_b` = `x`.`count_ab`