Ver originalSELECT c.*, p.* FROM clientes c INNER JOIN (SELECT MAX(fecha_pago) AS pago, codigoFROM pagosgroup by codigo) as p ON c.codigo = p.codigo WHERE c.sector='".$sector."' GROUP BY p.codigo ORDER BY direccion