SQL:
SELECT ship.uid as shipuid, ship.id as shipid, ship.name as shipname, ship.owner as shipowner, count(gal.*) as images FROM ships as ship LEFT JOIN ship_gallery as gal ON (ship.id = gal.ship_id) WHERE ship.active = 1 GROUP BY ship.uid, ship.id, ship.name, ship.owner ORDER BY ship.uid, ship.name ASC