UPDATE room SET is_max = 1 WHERE delete_mark = 0 AND ( SELECT COUNT(*) FROM ( SELECT COUNT(*) FROM base_student_school_roll t1 LEFT JOIN room_bed t3 ON t1.user_id = t3.student_user_id WHERE t3.room_id = room.id AND t3.delete_mark = 0 GROUP BY t1.class_id ) class_data ) > 1 UPDATE room SET is_max = 0 WHERE delete_mark = 0 AND 1 >( SELECT COUNT(*) FROM ( SELECT COUNT(*) FROM base_student_school_roll t1 LEFT JOIN room_bed t3 ON t1.user_id = t3.student_user_id WHERE t3.room_id = room.id AND t3.delete_mark = 0 GROUP BY t1.class_id ) class_data )