Ver Mensaje Individual
  #4  
Antiguo 05-04-2017
Avatar de RONPABLO
[RONPABLO] RONPABLO is offline
Miembro Premium
 
Registrado: oct 2004
Posts: 1.514
Reputación: 21
RONPABLO Va por buen camino
Cita:
Empezado por SmartZooft Ver Mensaje
Hola a todos, tengo dos tablas: Clientes e Historia. Clientes es papá de historia, lo que quiero es borrar de historia los registros donde el cliente tenga fecha menor al año 2015, lo tengo así...

Código SQL [-]
DELETE FROM HISTORIA
where 
ID_CLIENTE IN (SELECT ID_CLIENTE FROM CLIENTES WHERE fecha < '01/01/2015')
AND
ID_HISTORIA IN (SELECT ID_HISTORIA FROM CLIENTES WHERE fecha < '01/01/2015')


pero no sé si funciona porque se queda en el limbo pensando, alguien sabe si se puede optimizar ésta sentencia?

muchas gracias a todos.
Cabe anotar que la clausula "IN" es una clausuala poco recomendable para ser usada en tablas con muchos registros, podrías acomodar mejor la consulta con un exists de la siguiente forma (primero hacer la consulta y si los resultados que se ven son los esperado proceder con el delete)

Código SQL [-]
select * from FROM HISTORIA h
where 
exists (SELECT h1.ID_CLIENTE FROM CLIENTES h1 WHERE h1.ID_CLIENTE = h.ID_CLIENTE and h1.ID_HISTORIA = h.ID_HISTORIA and h1.fecha < '01/01/2015')


Y si funciona bien

Código SQL [-]
delete from FROM HISTORIA h
where 
exists (SELECT h1.ID_CLIENTE FROM CLIENTES h1 WHERE h1.ID_CLIENTE = h.ID_CLIENTE and h1.ID_HISTORIA = h.ID_HISTORIA and h1.fecha < '01/01/2015')
__________________
"Como pasa el tiempo..... ayer se escribe sin H y hoy con H"
Responder Con Cita