Diferent result between query and application (MySQL) I have this scenario:

I have this query:

SELECT min(id) 
  FROM ( SELECT c.id
              , id_Cliente 
           FROM creditos AS c
          INNER JOIN  creditos_ren AS r 
                  ON r.id = (SELECT rr.id 
                               FROM creditos_ren AS rr 
                              WHERE rr.id_credito= c.id 
                                AND rr.cancelado=false 
                                AND rr.fecha_vto&gt;='2018-07-15' 
                                AND rr.fecha_vto&lt;'2018-07-24' 
                              ORDER BY rr.fecha_vto  
                              LIMIT 1 
                            )      
          LEFT JOIN  creditos_ren AS rx 
                 ON rx.id =(SELECT xx.id 
                              FROM creditos_ren AS xx 
                             WHERE xx.id_credito=c.id 
                               AND xx.id != r.id 
                               AND xx.fecha_vto &gt;= r.fecha_vto  
                               AND xx.cancelado=false 
                             ORDER BY xx.fecha_vto 
                             LIMIT 1
                           )  
          WHERE c.id NOT IN ( SELECT d.id_credito 
                                FROM campanas_instancia_lineas AS d
                            ) 
            AND c.gestion_local = 0 
            AND c.enumerador IN ('CR_NORMAL','CR_ENTREGADO','CR_NUEVO','CR_MOROSO') 
          ORDER BY r.fecha_vto
                 , c.total_vencido desc
                 , rx.fecha_vto 
        ) AS p 
 GROUP BY id_Cliente;


When run the query in the WorkBench, the result for example are 260 matches.

But, when I run the same query in the application the result are 497 matches.

I use javaEE with nativeQuery.

I try to use VIEWs, with the same result

If someone help me, please.

Thanks in advance
Make sure both approaches are looking the same DATABASE.

Further debugging:


Turn on the general log.
Run the query both ways.
Turn off the general log.
Compare the two queries; there must be something different.
Figure out what is changing what.


Meanwhile, these indexes may help performance:

creditos_ren:  INDEX(id_credito, canceladom fecha_vto, id)
creditos:      INDEX(gestion_local, enumerador)


What table is id_Cliente in?