SELECT a.room_id, IFNULL(t1.count1, 0) FROM a LEFT JOIN (SELECT room_name, COUNT(*) AS count1 FROM b GROUP BY room_name) t1 ON a.`room_id` = t1.room_name 看看是你要的效果吗