Ver Mensaje Individual
  #8 (permalink)  
Antiguo 26/03/2012, 12:58
earmoa
 
Fecha de Ingreso: julio-2009
Mensajes: 6
Antigüedad: 14 años, 9 meses
Puntos: 0
Respuesta: Comparar 2 consultas

bueno ver si estoy mal con mi sql
Código PHP:
$m_cons "SELECT CAST((if(dayofweek(fec_ven)=1,fec_ven+1,if(dayofweek(fec_ven)=7,fec_ven+2,fec_ven))) as DATE) as fecha_real, fec_ven,saldo, week(fec_ven) as semana, if(weekday(fec_ven)=5,day(fec_ven)+2,if(weekday(fec_ven)=6,day(fec_ven)+1,day(fec_ven))) as dia,SUM(1) as can,SUM(saldo) as sal,fec_otro from dbcclied where fec_ven between '$f_date1' and '$f_date2' and cabecera=0 and saldo>0 group by dia order by dia";
$m_const mysql_query($m_cons$conexion) or die(mysql_error());

$m_cons2 "SELECT CAST((if(dayofweek(fec_otro)=1,fec_otro+1,if(dayofweek(fec_otro)=7,fec_otro+2,fec_otro))) as DATE) as fecha_real, fec_otro,saldo, week(fec_otro) as semana,       if(weekday(fec_otro)=5,day(fec_otro)+2,if(weekday(fec_otro)=6,day(fec_otro)+1,day(fec_otro))) as dia,SUM(1) as can,SUM(saldo) as sal,fec_otro from dbcclied where fec_otro between '$f_date1' and '$f_date2' and cabecera=0 and saldo>0 and fec_ven not between '$f_date1' and '$f_date2' group by dia order by dia "
$m_const2 mysql_query($m_cons2$conexion) or die(mysql_error()); 
como veran la consulta es a la misma tabla pero con diferentes parametros, me gustaria saber si hay alguna manera de unir estas dos consultas, cosa que no he podido hacerlo, la manera en que recorro e imprimo los resultados es esta:
Código PHP:
while ($pag2 = mysql_fetch_array($m_const) and $pag3 = mysql_fetch_array($m_const2)){
?>
     <tr>
        <td class="left" style="font-size:13px">        
        <?php
            $fch
=explode("-"$pag2["fecha_real"]);
            
$fecha1=$fch[2]."/".$fch[1]."/".$fch[0];
             echo 
$fecha1;?>
        </td>
        <td align="right" class="right" style="font:normal 13px verdana, arial, helvetica, sans-serif">
        <?php

        
echo number_format($pag2["can"]); 

        
?>
        <td align="right" class="right" style="font:normal 13px verdana, arial, helvetica, sans-serif">
        <?php
            
echo number_format($pag2["sal"]); 
        
?>
        </td>                
        <td align="right" class="right" style="font:normal 13px verdana, arial, helvetica, sans-serif">
        <?php
        
if ($pag2['fecha_real'] == $pag3['fecha_real'] ){
            echo 
$pag3["can"]; 
        }else{
            echo 
'0'
            
        }
?>
        <td align="right" class="right" style="font:normal 13px verdana, arial, helvetica, sans-serif">
        <?php
        
if ($pag2['fecha_real'] == $pag3['fecha_real'] ){
            echo 
number_format($pag3["sal"]); 
        }else{
            echo 
'0'
        }
?>
        </td>
       </tr>
    </table>
..bueno estoy seguro que eso se puede mejorar, al imprimir los resultados de esta manera al recorrer, cuando encuentra que en la segunda consulta no tiene una fecha igual al de la primera consulta, me pone el cero, pero al seguir recorriendo ya me pone cero los demas campos de la segunda consulta, hasta ahora no se como resolverlo