im trying to get a SUM from 2 diff queries and then subtract them but i dont know how. im trying something like this

  <?php 
   include("config.php"); 
   $link=Conectarse(); 
   $suma=mysql_query("select SUM(cantidad) as total from 'movimientos' where tipo='0'",$link); 
   $resta=mysql_query("select SUM(cantidad) as total from 'movimientos' where tipo='1'",$link); 

?>

but it's possible to make something like

$total = $suma - $resta; ??

    You could probably do it in one query:

    $sql = "SELECT SUM(cantidad) - 
    (SELECT SUM(cantidad) FROM movimientos WHERE tipo = 1) AS total
    FROM movimientos WHERE tipo = 0";
    $result = mysql_query($sql);
    $total = mysql_result($result, 0);
    

    (You should add some error-handling code, that's just the basic idea.)

      wow great i was trying to do that but i could figure out the correct sintax
      ty

        i just realize that that was not the problem, i actually have this now

          <?php 
           include("config.php"); 
           $link=Conectarse(); 
           $result=mysql_query("select * from movimientos"); 
        
           $sql = "SELECT SUM(cantidad) - (SELECT SUM(cantidad) FROM movimientos WHERE tipo = 1) AS total FROM movimientos WHERE tipo = 0"; 
           $result = mysql_query($sql); 
           $total = mysql_result($result, 0); 
        
        ?>

        so i can just show results for either $result or $total but not both :S

        $result have this above

        <?php       
        
        while($row = mysql_fetch_array($result)){
        code
        }
          Write a Reply...