Foros del Web » Programación para mayores de 30 ;) » Bases de Datos General » Mysql »

[SOLUCIONADO] sumar resultado de dos select

Estas en el tema de sumar resultado de dos select en el foro de Mysql en Foros del Web. TABLA 1 campos entrega | fecha (tipo DATE) 1000 | 2015-09-14 2000 | 2015-09-15 3000 | 2015-09-16 TABLA 2 campos entrega | fecha (tipo TIMESTAMP) ...
  #1 (permalink)  
Antiguo 29/09/2015, 12:25
Avatar de Fernarey1810  
Fecha de Ingreso: noviembre-2008
Mensajes: 214
Antigüedad: 15 años, 5 meses
Puntos: 1
sumar resultado de dos select

TABLA 1 campos
entrega | fecha (tipo DATE)
1000 | 2015-09-14
2000 | 2015-09-15
3000 | 2015-09-16

TABLA 2 campos
entrega | fecha (tipo TIMESTAMP)
1000 | 2015-09-14 06:30:00
2000 | 2015-09-15 06:30:00
3000 | 2015-09-16 06:30:00

Si hago lo siguiente
Código MySQL:
Ver original
  1. select ((select sum(entrega) from entregas) + (select sum(entrega) from entregas_ventas )) AS total

Suma perfecto todo, aunque yo quiero que sea entre fechas, asi:
Código MySQL:
Ver original
  1. select ((select sum(entrega) from entregas where fecha between '2015-09-14' and '2015-09-16') + (select sum(entrega) from entregas_ventas where fecha between '2015-09-16' and '2015-09-16')) AS total

ESTO ME DA NULL A QUE SE DEBE? TIENE ALGO QUE VER QUE UN CAMPO SEA DATE Y EL OTRO TIMESTAMP? creeeria que no influye eso debe ser otra cosa
  #2 (permalink)  
Antiguo 29/09/2015, 14:52
Avatar de Libras
Colaborador
 
Fecha de Ingreso: agosto-2006
Ubicación: En la hermosa perla de occidente
Mensajes: 7.412
Antigüedad: 17 años, 8 meses
Puntos: 774
Respuesta: sumar resultado de dos select

ya probaste tus querys por separado???
__________________
What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me
  #3 (permalink)  
Antiguo 29/09/2015, 16:10
Avatar de Fernarey1810  
Fecha de Ingreso: noviembre-2008
Mensajes: 214
Antigüedad: 15 años, 5 meses
Puntos: 1
Respuesta: sumar resultado de dos select

Ambas consultas funcionan perfectamente. El problema era que si una de las 2 DA NULL, al sumar por ej 1000+NULL daba NULL.
Existe una funcion llamada IFNULL(a,b), si A es NULL devuelve B. En este caso al darme null la busqueda pongo que me de 0. Esta es la consulta correcta:

Código MySQL:
Ver original
  1. select ((select ifnull(sum(entrega),0) from entregas where fecha between '2015-07-07' and '2015-07-11') + (select ifnull(sum(entrega),0) from entregas_ventas where fecha between '2015-07-07' and '2015-07-11')) AS total

Etiquetas: campo, fecha, resultado, select, tabla
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 17:44.